Other helpers
within, getQueriesForElement
within (also available as getQueriesForElement alias) performs queries scoped to given element.
Please note that additional render specific operations like update, 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
Useful function for testing components that use hooks API. By default, any render, update, fireEvent, and waitFor calls are wrapped by this function, so you don't need to wrap it manually. This method is re-exported from react-test-renderer.
Consult our Understanding Act function document for more understanding of its intricacies.
cleanup
Unmounts React trees that were mounted with render and clears the screen variable that holds the latest render output.
This is done automatically if the testing framework you're using supports the afterEach global (like mocha, Jest, and Jasmine). If not, you'll need to do manual cleanups after each test.
For example, if you're using the jest testing framework, 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 that aren't "idempotent" (which can lead to difficult-to-debug errors).
