testing-library /react-testing-library
🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
★ 19,682⑂ 1,171👁 137⑃ 549● 840▤ 208
+1
日增长
19,682
stars
1,171
forks
137
watchers
549
prs
840
issues
208
releases
README
React Testing Library
Simple and complete React DOM testing utilities that encourage good testing
practices.
Read The Docs |
Edit the docs
[![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]](contributors)
[![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]
Table of Contents
- The problem
- The solution
- Installation
- Suppressing unnecessary warnings on React DOM 16.8
- Examples
- Basic Example
- Complex Example
- More Examples
- Hooks
- Guiding Principles
- Docs
- Issues
- 🐛 Bugs
- 💡 Feature Requests
- ❓ Questions
- Contributors
- LICENSE
The problem
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 solution
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]
Installation
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/dom
or
for installation via [yarn][yarn]
yarn add --dev @testing-library/react @testing-library/dom
This 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@12
You may also be interested in installing @testing-library/jest-dom so you can
use the custom jest matchers.
Docs
Suppressing unnecessary warnings on React DOM 16.8
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(...).
IfTop issues
#1209Support for React Server Components156 comments · 2023-05-09#1197Update to v14 breaks @testing-library/user-event on Vitest33 comments · 2023-03-21#1375@testing-library/react behaves differently when dealing with Suspense in React 18 and React 1932 comments · 2025-01-09#1120renderHook Server24 comments · 2022-09-06#1216Receiving 'event was not wrapped in act' warning after upgrading to React 1823 comments · 2023-05-23#974Log from .logTestingPlaygroundURL is to big and not working18 comments · 2021-10-06#865Flaky tests: Asynchronous queries can return unmounted elements18 comments · 2021-01-18#1233How to wrap a hook with a Provider with prop passing?16 comments · 2023-08-16
Latest release
v16.3.3 v16.3.3 2026-08-2716.3.3 (2026-08-27) Bug Fixes Avoid act() re-entrant when dispatching events (1468) (20ce75f)