Testing React Native Apps

    Get a deeper insight into testing a working React Native app example by reading the following series: Part 1: Jest – Snapshot come into play and .

    Starting from react-native version 0.38, a Jest setup is included by default when running . The following configuration should be automatically added to your package.json file:

    Run yarn test to run tests with Jest.

    tip

    If you are upgrading your react-native application and previously used the jest-react-native preset, remove the dependency from your package.json file and change the preset to react-native instead.

    Let’s create a snapshot test for a small intro component with a few views and text components and some styles:

    Intro.js

    1. import React, {Component} from 'react';
    2. import {StyleSheet, Text, View} from 'react-native';
    3. class Intro extends Component {
    4. render() {
    5. return (
    6. <View style={styles.container}>
    7. <Text style={styles.welcome}>Welcome to React Native!</Text>
    8. <Text style={styles.instructions}>
    9. This is a React Native snapshot test.
    10. </Text>
    11. </View>
    12. }
    13. }
    14. const styles = StyleSheet.create({
    15. container: {
    16. alignItems: 'center',
    17. backgroundColor: '#F5FCFF',
    18. flex: 1,
    19. justifyContent: 'center',
    20. instructions: {
    21. color: '#333333',
    22. marginBottom: 5,
    23. textAlign: 'center',
    24. },
    25. welcome: {
    26. fontSize: 20,
    27. margin: 10,
    28. textAlign: 'center',
    29. },
    30. });
    31. export default Intro;

    Now let’s use React’s test renderer and Jest’s snapshot feature to interact with the component and capture the rendered output and create a snapshot file:

    1. import React from 'react';
    2. import renderer from 'react-test-renderer';
    3. import Intro from '../Intro';
    4. test('renders correctly', () => {
    5. const tree = renderer.create(<Intro />).toJSON();
    6. expect(tree).toMatchSnapshot();
    7. });

    When you run yarn test or jest, this will produce an output file like this:

    __tests__/__snapshots__/Intro-test.js.snap

    The next time you run the tests, the rendered output will be compared to the previously created snapshot. The snapshot should be committed along with code changes. When a snapshot test fails, you need to inspect whether it is an intended or unintended change. If the change is expected you can invoke Jest with jest -u to overwrite the existing snapshot.

    The code for this example is available at .

    The preset sets up the environment and is very opinionated and based on what we found to be useful at Facebook. All of the configuration options can be overwritten just as they can be customized when no preset is used.

    react-native ships with a Jest preset, so the jest.preset field of your should point to react-native. The preset is a node environment that mimics the environment of a React Native app. Because it doesn’t load any DOM or browser APIs, it greatly improves Jest’s startup time.

    The option can be used to specify which files shall be transformed by Babel. Many react-native npm modules unfortunately don’t pre-compile their source code before publishing.

    By default the jest-react-native preset only processes the project’s own source files and react-native. If you have npm dependencies that have to be transformed you can customize this configuration option by including modules other than react-native by grouping them and separating them with the | operator:

    1. {
    2. "transformIgnorePatterns": [
    3. "node_modules/(?!(react-native|my-project|react-native-button)/)"
    4. ]
    5. }

    transformIgnorePatterns will exclude a file from transformation if the path matches against any pattern provided. Splitting into multiple patterns could therefore have unintended results if you are not careful. In the example below, the exclusion (also known as a negative lookahead assertion) for foo and bar cancel each other out:

    1. {
    2. "transformIgnorePatterns": ["node_modules/(?!foo/)", "node_modules/(?!bar/)"] // not what you want
    3. }

    If you’d like to provide additional configuration for every test file, the can be used to specify setup scripts.

    The can be used to map a module path to a different module. By default the preset maps all images to an image stub module but if a module cannot be found this configuration option can help:

    The Jest preset built into react-native comes with a few default mocks that are applied on a react-native repository. However, some react-native components or third party components rely on native code to be rendered. In such cases, Jest’s manual mocking system can help to mock out the underlying implementation.

    For example, if your code depends on a third party native video component called react-native-video you might want to stub it out with a manual mock like this:

    1. jest.mock('react-native-video', () => 'Video');

    This will render the component as <Video {...props} /> with all of its props in the snapshot output. See also caveats around Enzyme and React 16.

    Sometimes you need to provide a more complex manual mock. For example if you’d like to forward the prop types or static fields of a native component to a mock, you can return a different React component from a mock through this helper from jest-react-native:

    1. jest.mock('path/to/MyNativeComponent', () => {
    2. const mockComponent = require('react-native/jest/mockComponent');
    3. return mockComponent('path/to/MyNativeComponent');

    Or if you’d like to create your own manual mock, you can do something like this:

    If you end up mocking the same modules over and over it is recommended to define these mocks in a separate file and add it to the list of setupFiles.