Custom Queries

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).


Custom queries can be added to React Testing Library's render method by adding queries to the options config object. See the render options.

Custom queries are different than custom render methods.

// test-utils.js
const domTestingLib = require('@testing-library/dom')
const { queryHelpers } = domTestingLib
export const queryByTestId = queryHelpers.queryByAttribute.bind(
export const queryAllByTestId = queryHelpers.queryAllByAttribute.bind(
export function getAllByTestId(container, id, {
const els = queryAllByTestId(container, id,
if (!els.length) {
throw queryHelpers.getElementError(
`Unable to find an element by: [data-test-id="${id}"]`,
return els
export function getByTestId(...args) {
const result = getAllByTestId(...args)
if (result.length > 0) {
return result[0]
return null
// re-export with overrides
module.exports = {


The buildQueries helper allows you to create custom queries with all standard variants of 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(node: HTMLElement)

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.

// <div>
// Hello
// World !
// </div>
const text = getNodeText(container.querySelector('div')) // "Hello World !"

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:

// <input type="submit" value="Send data" />
// <input type="button" value="Push me" />
const submitText = getNodeText(container.querySelector('input[type=submit]')) // "Send data"
const buttonText = getNodeText(container.querySelector('input[type=button]')) // "Push me"
These elements use the attribute `value` and display its value to the user.
Last updated on by Alex Krolick