This guide covers how to test React Native components using Harness's rendering API, which is inspired by React Native Testing Library.
Native module testing isn't just about testing JavaScript APIs - it's also about testing custom Fabric views and ensuring props are properly handled on the native side. When you create native components, you need to verify that props are correctly passed through to the native layer and that your views render as expected on actual iOS and Android devices.
Harness provides a render function that allows you to render custom native components directly on the device/simulator for testing. This enables you to test real native view behavior, prop handling, and rendering that's impossible to verify in a Node.js environment like Jest. The rendered components appear as an overlay during test execution and are automatically cleaned up after each test.
Renders a React Native element and returns utilities to interact with it.
timeout - Timeout in milliseconds to wait for the component to mount. Default is 1000ms.
wrapper - A React component to wrap the rendered element. Useful for adding context providers.
rerender - Updates the rendered component with new props without unmounting. The component's internal state is preserved.
unmount - Manually unmounts the component. This is optional as components are automatically cleaned up after each test.
Components are automatically cleaned up after each test via an afterEach hook. You don't need to call unmount() unless you want to clean up early.
While Harness's render API is inspired by React Native Testing Library, there are some important differences:
render() function returns { rerender, unmount }render() and rerender() functionswrapper optiongetByText, findByRole, etc. as it focuses on testing native behavior rather than component structurefireEvent or user interaction helpersrender calls are awaitable and resolve when the component is mounted and laid outrerender is also awaitable and resolves when the component has updated