render API
render function
The render function is the entry point for writing React Native Testing Library tests. It deeply renders the given React element and returns helpers to query the output components' structure. This function is async by default and uses async act internally to ensure all pending React updates are executed during rendering, making it compatible with async React features like Suspense boundary or use() hook.
When using React context providers, like Redux Provider, you'll likely want to wrap rendered component with them. In such cases, it's convenient to create your own custom
rendermethod. Follow this great guide on how to set this up.
Options
The behavior of the render method can be customized by passing various options as a second argument of the RenderOptions type:
wrapper
This option allows you to wrap the tested component, passed as the first option to the render() function, in an additional wrapper component. This is useful for creating reusable custom render functions for common React Context providers.
createNodeMock
This option allows you to pass createNodeMock option to the renderer's create() method in order to allow for custom mock refs. This option is passed through to Test Renderer.
Test Renderer automatically enforces React Native's requirement that text strings must be rendered within a <Text> component. If you try to render a string value under components other than <Text> (e.g., under <View>), it will throw an Invariant Violation: Text strings must be rendered within a <Text> component error, matching React Native's runtime behavior.
This validation is always enabled and cannot be disabled, ensuring your tests accurately reflect how your code will behave in production.
Result
The render function returns a promise that resolves to the same queries and utilities as the screen object. We recommend using the screen object for queries and the lifecycle methods from the render result when needed.
See this article from Kent C. Dodds for more details.
Query results and element references use the HostElement type from Test Renderer. If you need to type element variables, you can import this type directly from test-renderer.
