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 using Circle, a continuous integration system. Circle 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. Note that brew may not install the version needed to run the tests. For best results, use the same version as the GitHub builds, which can be found in the
.circleci folder in the root of the repo.
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 Circle build passes.
The same tests discussed above for iOS will also run on tvOS. In the RNTester Xcode project, select the RNTester-tvOS target, and you can follow the same steps above to run the tests in Xcode.
You can run Apple TV unit and integration tests locally by running the following in the command line on macOS:
$ cd react-native $ ./scripts/objc-test-tvos.sh (make sure the line `TEST="test"` is uncommented)
Finally, make sure end-to-end tests run successfully by executing the following script:
$ cd react-native $ ./scripts/test-manual-e2e.sh
Updating the Documentation
If you are adding new functionality or introducing a change in behavior, we will ask you to update the documentation to reflect your changes. The docs are hosted as part of the React Native website. The website itself is hosted on GitHub Pages and is automatically generated from the Markdown sources.
To update the documentation, you will need to clone the
facebook/react-native-website repository, make your changes in the
docs/ directory, and then send a pull request. For lightweight changes to a single file, you may also click on "Edit" at the top of any doc right here on the website.