🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends] [![MIT License][license-badge]][license] ![All Contributors][all-contributors-badge] [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc] [![Discord][discord-badge]][discord]
[![Watch on GitHub][github-watch-badge]][github-watch] [![Star on GitHub][github-star-badge]][github-star] [![Tweet][twitter-badge]][twitter]
You want to write maintainable tests for your React components. As a part of this goal, you want your tests to avoid including implementation details of your components and rather focus on making your tests give you the confidence for which they are intended. As part of this, you want your testbase to be maintainable in the long run so refactors of your components (changes to implementation but not functionality) don't break your tests and slow you and your team down.
The React Testing Library is a very lightweight solution for testing React
components. It provides light utility functions on top of react-dom and
react-dom/test-utils, in a way that encourages better testing practices. Its
primary guiding principle is:
[The more your tests resemble the way your software is used, the more confidence they can give you.][guiding-principle]
This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's devDependencies.
Starting from RTL version 16, you'll also need to install
@testing-library/dom:
npm install --save-dev @testing-library/react @testing-library/domor
for installation via [yarn][yarn]
yarn add --dev @testing-library/react @testing-library/domThis library has peerDependencies listings for react, react-dom and
starting from RTL version 16 also @testing-library/dom.
React Testing Library versions 13+ require React v18. If your project uses an older version of React, be sure to install version 12:
npm install --save-dev @testing-library/react@12
yarn add --dev @testing-library/react@12You may also be interested in installing @testing-library/jest-dom so you can
use the custom jest matchers.
There is a known compatibility issue with React DOM 16.8 where you will see the following warning:
Warning: An update to ComponentName inside a test was not wrapped in act(...).If you cannot upgrade to React DOM 16.9, you may suppress the warnings by adding the following snippet to your test configuration (learn more):
// this is just a little hack to silence a warning that we'll get until we
// upgrade to 16.9. See also: https://github.com/facebook/react/pull/14853
const originalError = console.error
beforeAll(() => {
console.error = (...args) => {
if (/Warning.*not wrapped in act/.test(args[0])) {
return
}
originalError.call(console, ...args)
}
})
afterAll(() => {
console.error = originalError
})// hidden-message.js
import * as React from 'react'
// NOTE: React Testing Library works well with React Hooks and classes.
// Your tests will be the same regardless of how you write your components.
function HiddenMessage({children}) {
const [showMessage, setShowMessage] = React.useState(false)
return (
)
}
export default HiddenMessage………We recommend using Mock Service Worker library to declaratively mock API communication in your tests instead of stubbing
window.fetch, or relying on third-party adapters.
We're in the process of moving examples to the docs site
You'll find runnable examples of testing with different libraries in
the react-testing-library-examples codesandbox.
Some included are:
If you are interested in testing a custom hook, check out [React Hooks Testing Library][react-hooks-testing-library].
NOTE: it is not recommended to test single-use custom hooks in isolation from the components where it's being used. It's better to test the component that's using the hook rather than the hook itself. The
React Hooks Testing Libraryis intended to be used for reusable hooks/libraries.
[The more your tests resemble the way your software is used, the more confidence they can give you.][guiding-principle]
We try to only expose methods and utilities that encourage you to write tests that closely resemble how your React components are used.
Utilities are included in this project based on the following guiding principles:
react-dom,
utilities could be included even if they don't directly relate to
react-dom.Most importantly, we want React Testing Library to be pretty light-weight, simple, and easy to understand.
Looking to contribute? Look for the [Good First Issue][good-first-issue] label.
Please file an issue for bugs, missing documentation, or unexpected behavior.
[See Bugs][bugs]
Please file an issue to suggest new features. Vote on feature requests by adding a 👍. This helps maintainers prioritize what to work on.
[See Feature Requests][requests]
For questions related to using the library, please visit a support community instead of filing an issue on GitHub.
Thanks goes to these people ([emoji key][emojis]):
Support for React Server Components
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict mode
React act() warning persists with latest versions (React 18.3.1 + RTL 16.3.0 + Vitest 2.1.0)
On jest, `afterEach(cleanup)` is too late to unmount a component using `useSyncExternalStore` with asynchronous updating
Flaky Tests with user.type (missing characters)
React 19 - Objects are not valid as a React child
Provide example of how to test <details><summary>...
Having `afterEach(cleanup)` in the test setup file breaks exported `screen` module on Bun test
Unable to getByRole figure with accessible name / description in figcaption
Support for React.cache function