Custom Queries
DOM Testing Library
exposes many of the helper functions that are used to
implement the default queries. You can use the helpers to build custom queries.
For example, the code below shows a way to override the default testId
queries
to use a different data-attribute. (Note: test files would import
test-utils.js
instead of using DOM Testing Library
directly).
Note
Custom queries can be added to
React Testing Library
'srender
method by addingqueries
to the options config object. See the render options.
Custom queries are different than custom render methods.
buildQueries
The buildQueries
helper allows you to create custom queries with all the
standard queries in testing-library.
See the Add custom queries section of the custom render guide for example usage.
Using other assertion libraries
If you're not using jest, you may be able to find a similar set of custom assertions for your library of choice. Here's a list of alternatives to jest-dom for other popular assertion libraries:
If you're aware of some other alternatives, please make a pull request and add it here!
getNodeText
Returns the complete text content of an HTML element, removing any extra whitespace. The intention is to treat text in nodes exactly as how it is perceived by users in a browser, where any extra whitespace within words in the html code is not meaningful when the text is rendered.
This function is also used internally when querying nodes by their text content.
This enables functions like getByText
and queryByText
to work as expected,
finding elements in the DOM similarly to how users would do.
The function has a special behavior for some input elements: