React Testing Library provides functions to render components and then select internal elements. Those internal elements can then be checked using special matchers provided by jest-dom
, also see [[Test pure functions with Jest in React App]].
Both of these are preinstalled in a create-react-app project.
- React Testing Library’s
render
function renders the component. screen
object has many queries methods such asgetByText
that allows to select internal elements.- we use Jest’s matcher to check expectation:
import { render, screen } from '@testing-library/react';
test('should render heading when content specified', () => {
render(<Heading>Some heading</Heading>);
const heading = screen.getByText('Some heading');
expect(heading).toBeInDocument();
})
React Testing Library queries
Query is a method that selects a DOM element within the component being rendered.
Find element in different ways:
- ByRole: Queries elements by their role, see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles
- ByLabelText: Queries elements by their associated label.
- ByPlaceholderText
- ByText: by their text content
- ByDisplayValue: Queries input, textarea, and select elements by their value.
- ByAltText: Queries img elements by their alt attribute.
- ByTitle: title attribute
- ByTestId: by data-testid attribute
Different types of queries, each query type has a particular prefix on the query method name:
- getBy: Throws an error if a single element is not found. Used for synchronously getting a single element.
- getAllBy: Throws an error if at least one element is not found. Used for synchronously getting multiple elements.
- findBy: Throws an error if a single element is not found within a certain amount of time(1 second by default). Used for asynchronously getting a single element.
- findAllBy: Throws an error if at least one element is not found within a certain time(1 second by default). Used for asynchronously getting multiple elements.
- queryBy: This returns
null
if an element is not found. For checking that an element does not exist - queryAllBy: Same as queryBy, but returns an array of elements.
See https://testing-library.com/docs/queries/about/
Combine together, we get the query methods, such as getByText()
, queryByRole
,…
Notice that none of these queries references implementation details such as an element name, ID, or CSS class. If those implementation details change due to code refactoring, the tests shouldn’t break, which is precisely what we want.
Mathers
jest-dom contains lots of useful matchers for checking DOM elements, for example:
- toBeInDocument: verifies an element is in the DOM
- toBeChecked: checks whether an element is checked