React Testing Library Cheat Sheet

React Testing Library is a popular testing framework for React applications. It is designed to make testing React components more accessible and intuitive. The library provides a set of utilities that allow developers to test their components in a way that closely resembles how users interact with them.

One of the key features of React Testing Library is its focus on testing the behavior of components rather than their implementation details. This means that tests are written in a way that simulates user interactions with the component, rather than testing the internal state or structure of the component.

This cheat sheet provides an extensive list of React Testing Library methods and their descriptions. The methods are divided into different categories for easy reference.

Queries

MethodDescription
getByLabelTextReturns the first element that matches the given labelText attribute.
getByPlaceholderTextReturns the first element that matches the given placeholder attribute.
getByTextReturns the first element that matches the given text content.
getByAltTextReturns the first element that matches the given alt attribute.
getByTitleReturns the first element that matches the given title attribute.
getByRoleReturns the first element that matches the given role attribute.
getByTestIdReturns the first element that matches the given data-testid attribute.
queryByLabelTextReturns the first element that matches the given labelText attribute, or null if no elements match.
queryByPlaceholderTextReturns the first element that matches the given placeholder attribute, or null if no elements match.
queryByTextReturns the first element that matches the given text content, or null if no elements match.
queryByAltTextReturns the first element that matches the given alt attribute, or null if no elements match.
queryByTitleReturns the first element that matches the given title attribute, or null if no elements match.
queryByRoleReturns the first element that matches the given role attribute, or null if no elements match.
queryByTestIdReturns the first element that matches the given data-testid attribute, or null if no elements match.
getAllByLabelTextReturns an array of all elements that match the given labelText attribute.
getAllByPlaceholderTextReturns an array of all elements that match the given placeholder attribute.
getAllByTextReturns an array of all elements that match the given text content.
getAllByAltTextReturns an array of all elements that match the given alt attribute.
getAllByTitleReturns an array of all elements that match the given title attribute.
getAllByRoleReturns an array of all elements that match the given role attribute.
getAllByTestIdReturns an array of all elements that match the given data-testid attribute.

Matchers

MethodDescription
toBeDisabledMatches if the element is disabled.
toBeEnabledMatches if the element is enabled.
toBeEmptyMatches if the element has no children.
toBeInTheDocumentMatches if the element is present in the document.
toBeInvalidMatches if the element is invalid.
toBeRequiredMatches if the element is required.
toBeValidMatches if the element is valid.
toBeVisibleMatches if the element is visible.
toContainElementMatches if the element contains another element.
toContainHTMLMatches if the element contains the given HTML string.
toHaveAttributeMatches if the element has the given attribute.
toHaveClassMatches if the element has the given class.
toHaveFocusMatches if the element has focus.
toHaveFormValuesMatches if the element has the given form values.
toHaveStyleMatches if the element has the given style.
toHaveTextContentMatches if the element has the given text content.
toHaveValueMatches if the element has the given value.

Events

MethodDescription
fireEventFires the given event on the element.
clickFires a click event on the element.
dblClickFires a dblclick event on the element.
changeFires a change event on the element.
submitFires a submit event on the element.
focusFires a focus event on the element.
blurFires a blur event on the element.
keydownFires a keydown event on the element.
keyupFires a keyup event on the element.
keypressFires a keypress event on the element.
mouseDownFires a mousedown event on the element.
mouseUpFires a mouseup event on the element.
mouseEnterFires a mouseenter event on the element.
mouseLeaveFires a mouseleave event on the element.
touchStartFires a touchstart event on the element.
touchEndFires a touchend event on the element.
touchMoveFires a touchmove event on the element.

Async Utilities

MethodDescription
waitForWaits for the given callback function to return a truthy value.
waitForElementToBeRemovedWaits for the element to be removed from the document.
waitForDomChangeWaits for the DOM to change.
waitForElementWaits for the element to be present in the document.
waitForElementToBeVisibleWaits for the element to be visible in the document.
waitForElementToBeHiddenWaits for the element to be hidden in the document.

Mocking

MethodDescription
jest.fn()Creates a new mock function.
jest.spyOn()Creates a spy on the given object and method.
jest.mock()Mocks the given module.
jest.resetAllMocks()Resets all mock functions.
jest.clearAllMocks()Clears all mock function calls.

References

For more information on React Testing Library, please refer to the official documentation: https://testing-library.com/docs/react-testing-library/intro/