UI Testing
React Native Harness provides powerful UI testing capabilities that allow you to test your React Native components visually through screenshot testing and element interaction.
UI testing is an opt-in feature that requires installing an additional package. The @react-native-harness/ui package is not included by default and must be explicitly installed to use these features.
For iOS and Android, this package contains native code and requires rebuilding your app after installation. This code is automatically excluded from release builds and is only available in debug builds.
For Web, this package uses standard DOM APIs for interactions and a Playwright-based bridge for capturing screenshots.
Installation
To enable UI testing in your project, install the UI testing package:
After installation, rebuild your app since this package contains native code that needs to be compiled and linked.
Screen API
The screen object provides methods to query and interact with UI elements in your React Native application. It uses accessibility properties and test IDs to locate elements.
Element queries
Available query methods
Screenshot testing
Screenshot testing allows you to capture and compare visual snapshots of your UI components. This is particularly useful for detecting unexpected visual changes in your components.
Basic screenshot testing
Screenshot options
The toMatchImageSnapshot matcher accepts various options to customize the comparison:
Screenshot options reference
Element-specific screenshots
You can capture screenshots of specific elements instead of the entire screen:
Multiple screenshots in one test
User interaction testing
The userEvent API allows you to simulate user interactions with your components:
Troubleshooting
Anti-aliased text differences
Small differences in text rendering can cause false positives. Increase the threshold value for less sensitive comparison.
Dynamic content
For components with dynamic content (dates, random values), use ignoreRegions to exclude those areas from comparison.
