Other helpers
within
within performs queries scoped to given element.
Please note that additional render specific operations like rerender, unmount, debug, toJSON are not included.
Use cases for scoped queries include:
- queries scoped to a single item inside a FlatList containing many items
- queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation)
act
Wraps code that causes React state updates to ensure all updates are processed before assertions. By default any render, rerender, fireEvent, and waitFor calls are wrapped by this function, so there is no need to wrap it manually.
In v14, act is now async by default and always returns a Promise, making it compatible with async React features like Suspense boundary or use() hook. This ensures all pending React updates are executed before the Promise resolves.
Note: Even if your callback is synchronous, you should still use await act(...) as act now always returns a Promise.
Consult our Understanding Act function document for more understanding of its intricacies.
cleanup
Unmounts React trees that were mounted with render and clears screen variable that holds latest render output.
Please note that this is done automatically if the testing framework you're using supports the afterEach global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups after each test.
For example, if you're using the jest testing framework, then you would need to use the afterEach hook like so:
The afterEach(cleanup) call also works in describe blocks:
Failing to call cleanup when you've called render could result in a memory leak and tests which are not "idempotent" (which can lead to difficult to debug errors in your tests).
