Quick Start
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.
Prerequisites
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.
Installation
Install React Native Harness as a development dependency:
Configuration
The easiest way to get started is with our interactive setup wizard! It will guide you through the configuration process and handle most of the setup automatically.
Run the wizard from your project root:
The wizard will:
- Ask for your project type (Expo or React Native CLI)
- Help you select platforms (Android/iOS)
- Find available devices and simulators
- Generate bundle IDs and create configuration files
- Install required platform packages
- Set up Jest configuration
The wizard handles the complex configuration details for you, making setup much faster and less error-prone than manual configuration.
Manual Configuration (if wizard fails)
If the wizard doesn't work for your setup, you can configure Harness manually:
1. Install Platform Packages
Install the platform packages you need:
2. Create Harness Configuration
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.
For Expo projects, the entryPoint should be set to the path specified in the main property of package.json. The appRegistryComponentName is typically set to main for Expo apps.
3. Update Metro Configuration (Required)
Whether you use the wizard or manual setup, you still need to update your Metro configuration so React Native Harness can 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!
3. Configure Jest
The wizard creates a dedicated jest.harness.config.mjs file for your Harness tests. If you prefer manual setup, create a jest.harness.config.mjs file:
If you want to run both traditional Jest tests and Harness tests in the same app, you can use Jest's projects feature. The wizard creates a separate Jest config for Harness tests, but you can also configure this manually:
This way, you can keep your fast unit tests alongside your comprehensive in-app integration tests, and Jest will run them all together!
Writing Your First Test
Create a test file with a .harness.js or .harness.ts extension. Import testing utilities from react-native-harness instead of Jest:
Available Testing APIs
React Native Harness provides Jest-compatible APIs through react-native-harness:
Test Structure
describe(name, fn)- Group related testsit(name, fn)ortest(name, fn)- Define individual testsit.skip()ortest.skip()- Skip testsit.only()ortest.only()- Run only specific tests
Lifecycle Hooks
beforeAll(fn)- Run once before all testsafterAll(fn)- Run once after all testsbeforeEach(fn)- Run before each testafterEach(fn)- Run after each test
Assertions
Create 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.
Building Your App
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:
React Native Community CLI
Expo
Rock
Running Tests
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:
- Watch mode:
react-native-harness --watch --harnessRunner androidto automatically rerun tests when files change - Code coverage:
react-native-harness --coverage --harnessRunner androidto see how much of your code is tested - Run specific tests:
react-native-harness MyComponent.harness --harnessRunner androidto run only certain test files - Filter by name:
react-native-harness --testNamePattern="specific test" --harnessRunner androidto run tests matching a pattern
What's Next?
Congratulations! You now have React Native Harness set up and can write tests that run in real native environments.
