React Native Harness allows you to write Jest-style tests that run directly in your React Native app with full access to native modules. Let's get you set up in minutes.
React Native Harness provides a dedicated react-native-harness command that wraps the Jest CLI under the hood, giving you all the powerful features of Jest including watch mode, code coverage, filtering, and more. This means you get the full Jest experience with seamless integration for running tests in real native environments.
Make sure you have Jest installed in your project. Most React Native projects come with Jest by default, so you should be all set! If Jest isn't installed yet, check out the Jest Getting Started guide to get it up and running.
Install React Native Harness as a development dependency:
Create a rn-harness.config.mjs file in your project root:
The entryPoint and appRegistryComponentName properties tell React Native Harness how to locate and integrate with your React Native app. See the Configuration page for detailed information about these and all other configuration options.
Update your metro.config.js so React Native Harness will be able to use it to bundle its tests:
The withRnHarness function is a noop when you're not running Harness tests, so you don't need to worry about it affecting your app in production. It only kicks in when running tests!
Update your jest.config.js to use the React Native Harness preset:
If you want to run both traditional Jest tests and Harness tests in the same app, you can use Jest's projects feature. This lets you separate your regular unit tests from your in-app Harness tests:
This way, you can keep your fast unit tests alongside your comprehensive in-app integration tests, and Jest will run them all together!
Create a test file with a .harness.js or .harness.ts extension. Import testing utilities from react-native-harness instead of Jest:
React Native Harness provides Jest-compatible APIs through react-native-harness:
describe(name, fn) - Group related testsit(name, fn) or test(name, fn) - Define individual testsit.skip() or test.skip() - Skip testsit.only() or test.only() - Run only specific testsbeforeAll(fn) - Run once before all testsafterAll(fn) - Run once after all testsbeforeEach(fn) - Run before each testafterEach(fn) - Run after each testCreate expectations using expect(value) with matchers:
.toBe() - Strict equality.toEqual() - Deep equality.toBeTruthy() - Truthy values.toBeFalsy() - Falsy values.toContain() - Array/string contains.toHaveLength() - Length check.toMatch() - Regex match.toBeInstanceOf() - Instance type.toHaveProperty() - Object property...and many more Jest-compatible matchers.
Before running tests, you need to build your app in debug mode and install it on your emulator or simulator. React Native Harness will inject itself into your existing app, taking over access to all included native modules.
Follow your framework's documentation to build and install the debug variant:
Now you're ready to run your tests! Use the react-native-harness command with the --harnessRunner flag to specify which platform to run on:
If you don't provide the --harnessRunner flag, React Native Harness will use the runner specified in the defaultRunner property of your rn-harness.config.mjs file. If no defaultRunner is configured, you must explicitly provide the --harnessRunner flag.
Since the react-native-harness command wraps Jest CLI under the hood, you get all the powerful Jest CLI features out of the box:
react-native-harness --watch --harnessRunner android to automatically rerun tests when files changereact-native-harness --coverage --harnessRunner android to see how much of your code is testedreact-native-harness MyComponent.harness --harnessRunner android to run only certain test filesreact-native-harness --testNamePattern="specific test" --harnessRunner android to run tests matching a patternCongratulations! You now have React Native Harness set up and can write tests that run in real native environments.