You are viewing documentation for v14 alpha. This version is under active development and APIs may change.

render API

render function

async function render<T>(
  element: React.ReactElement<T>,
  options?: RenderOptions
): Promise<RenderResult>;

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.

import { render, screen } from '@testing-library/react-native';

test('basic test', async () => {
  await render(<MyApp />);
  expect(screen.getAllByRole('button', { name: 'start' })).toBeOnTheScreen();
});

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 render method. 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

wrapper?: React.ComponentType<any>,

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

createNodeMock?: (element: React.ReactElement) => object,

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.

Text string validation

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.

Type information

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.

Need React or React Native expertise you can count on?