screen object
The screen object offers a recommended way to access queries and utilities for the currently rendered UI.
This object is assigned after the render call and cleared after each test by calling cleanup. If no render call has been made in a given test, then it holds a special object and throws a helpful error on each property and method access.
...queries
The most important feature of screen is providing a set of helpful queries that allow you to find certain elements in the view hierarchy.
See Queries for a complete list.
Example
rerender
Re-render the in-memory tree with a new root element. This simulates a React update render at the root. If the new element has the same type (and key) as the previous element, the tree will be updated; otherwise, it will re-mount a new tree, in both cases triggering the appropriate lifecycle events.
This method is async and uses async act function internally to ensure all pending React updates are executed during updating, making it compatible with async React features like Suspense boundary or use() hook.
unmount
Unmount the in-memory tree, triggering the appropriate lifecycle events.
This method is async and uses async act function internally to ensure all pending React updates are executed during unmounting, making it compatible with async React features like Suspense boundary or use() hook.
Usually you should not need to call unmount as it is done automatically if your test runner supports afterEach hook (like Jest, mocha, Jasmine).
debug
Pretty prints deeply rendered component passed to render.
message option
You can provide a message that will be printed on top.
logs optional message and colored JSX:
mapProps option
You can use the mapProps option to transform the props that will be printed :
This will log the rendered JSX without the style props.
The children prop cannot be filtered out so the following will print all rendered components with all props but children filtered out.
This option can be used to target specific props when debugging a query (for instance, keeping only the children prop when debugging a getByText query).
You can also transform prop values so that they are more readable (e.g., flatten styles).
Or remove props that have little value when debugging tests, e.g. path prop for svgs
toJSON
Get the rendered component JSON representation, e.g. for snapshot testing.
container
Returns a pseudo-element container whose children are the elements you asked to render. This is the root container element from Test Renderer.
The container is safe to use and provides access to the entire rendered tree. It's useful when you need to query or manipulate the entire rendered output, similar to how container works in React Testing Library.
root
Returns the rendered root host element, or null if nothing was rendered. This is the first child of the container, which represents the actual root element you rendered.
This API is primarily useful for component tests, as it allows you to access root host view without using *ByTestId queries or similar methods.
In rare cases where your root element is a React.Fragment with multiple children, the container will have more than one child, and root will return only the first one. In such cases, use container.children to access all rendered elements.
