jest-dom
jest-dom
is a companion library for React Testing Library
that
provides custom DOM element matchers for Jest
npm install --save-dev @testing-library/jest-dom
Then follow usage section from jest-dom's documentation to add the matchers to Jest.
<span data-testid="not-empty"><span data-testid="empty"></span></span>
<div data-testid="visible">Visible Example</div>
expect(queryByTestId(container, 'not-empty')).not.toBeEmpty()
expect(getByText(container, 'Visible Example')).toBeVisible()
Note: when using some of these matchers, you may need to make sure you use a query function (like
queryByTestId
) rather than a get function (likegetByTestId
). Otherwise theget*
function could throw an error before your assertion.
Check out jest-dom's documentation for a full list of available matchers.