This document is about running tests on React Native itself. If you're interested in testing a React Native app, check out the React Native Tutorial on the Jest website.
Running Tests and Contributing
The React Native repo has several tests you can run to verify you haven't caused a regression with your PR. These tests are run with the Travis and CircleCI continuous integration systems, which will automatically annotate pull requests with the test results.
Whenever you are fixing a bug or adding new functionality to React Native, you should add a test that covers it. Depending on the change you're making, there are different types of tests that may be appropriate.
$ cd react-native $ npm test
The tests themselves live in the
__tests__ directories of the files they test. See
TouchableHighlight-test.js for a basic example.
You should also make sure your code passes Flow tests. These can be run using:
$ cd react-native $ npm run flow
The Android unit tests do not run in an emulator. They just use a normal Java installation. The default macOS Java install is insufficient, you may need to install Java 8 (JDK8). You can type
javac -version in a terminal to see what version you have:
$ javac -version javac 1.8.0_111
The version string
1.8.x_xxx corresponds to JDK 8.
You also need to install the Buck build tool.
To run the Android unit tests:
$ cd react-native $ ./scripts/run-android-local-unit-tests.sh
It's a good idea to add an Android unit test whenever you are working on code that can be tested by Java code alone. The Android unit tests live under
ReactAndroid/src/tests, so you can browse through that directory for good examples of tests.
To run the integration tests, you need to install the Android NDK. See Prerequisites.
You also need to install the Buck build tool.
We recommend running the Android integration tests in an emulator, although you can also use a real Android device. It's a good idea to keep the emulator running with a visible window. That way if your tests stall, you can look at the emulator to debug.
Some devices and some emulator configurations may not work with the tests. We do maintain an emulator configuration that works, as the standard for testing. To run this emulator config:
$ cd react-native $ ./scripts/run-android-emulator.sh
Once you have an emulator running, to run the integration tests:
$ cd react-native $ ./scripts/run-android-local-integration-tests.sh
The integration tests should only take a few minutes to run on a modern developer machine.
ReactAndroid/src/androidTest, so you can browse through that directory for good examples of tests.
React Native provides facilities to make it easier to test integrated components that require both native and JS components to communicate across the bridge. The two main components are
RCTTestRunner sets up the ReactNative environment and provides facilities to run the tests as
XCTestCases in Xcode (
runTest:module is the simplest method).
RCTTestModule is exported to JS as
The tests themselves are written in JS, and must call
TestModule.markTestCompleted() when they are done, otherwise the test will timeout and fail. Test failures are primarily indicated by throwing a JS exception. It is also possible to test error conditions with
runTest:module:initialProps:expectErrorBlock: which will expect an error to be thrown and verify the error matches the provided criteria.
See the following for example usage and integration points:
You can run integration tests locally with cmd+U in the IntegrationTest and RNTester apps in Xcode, or by running the following in the command line on macOS:
$ cd react-native $ ./scripts/objc-test-ios.sh
Your Xcode install will come with a variety of Simulators running the latest OS. You may need to manually create a new Simulator to match what the
XCODE_DESTINATIONparam in the test script.
A common type of integration test is the snapshot test. These tests render a component, and verify snapshots of the screen against reference images using
TestModule.verifySnapshot(), using the
FBSnapshotTestCase library behind the scenes. Reference images are recorded by setting
recordMode = YES on the
RCTTestRunner, then running the tests. Snapshots will differ slightly between 32 and 64 bit, and various OS versions, so it's recommended that you enforce tests are run with the correct configuration. It's also highly recommended that all network data be mocked out, along with other potentially troublesome dependencies. See
SimpleSnapshotTest for a basic example.
If you make a change that affects a snapshot test in a PR, such as adding a new example case to one of the examples that is snapshotted, you'll need to re-record the snapshot reference image. To do this, simply change to
_runner.recordMode = YES; in RNTester/RNTesterSnapshotTests.m, re-run the failing tests, then flip record back to
NO and submit/update your PR and wait to see if the Travis build passes.
Finally, make sure end-to-end tests run successfully by executing the following script:
$ cd react-native $ ./scripts/test-manual-e2e.sh