Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-testing-library

> 前端框架
Open source

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.

19.6K stars0 likes2 views
WebsiteGitHub

About

🐐 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]

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:

bash
npm install --save-dev @testing-library/react @testing-library/dom

or

for installation via [yarn][yarn]

bash
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:

bash
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(...).

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):

javascript
// 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
})

Examples

Basic Example

javascript
// 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
…

Complex Example

…
…

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.

More Examples

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:

  • react-redux
  • react-router
  • react-context

Hooks

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 Library is intended to be used for reusable hooks/libraries.

Guiding Principles

[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:

  1. If it relates to rendering components, it deals with DOM nodes rather than component instances, nor should it encourage dealing with component instances.
  2. It should be generally useful for testing individual React components or full React applications. While this library is focused on react-dom, utilities could be included even if they don't directly relate to react-dom.
  3. Utility implementations and APIs should be simple and flexible.

Most importantly, we want React Testing Library to be pretty light-weight, simple, and easy to understand.

Docs

Read The Docs | Edit the docs

Issues

Looking to contribute? Look for the [Good First Issue][good-first-issue] label.

🐛 Bugs

Please file an issue for bugs, missing documentation, or unexpected behavior.

[See Bugs][bugs]

💡 Feature Requests

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]

❓ Questions

For questions related to using the library, please visit a support community instead of filing an issue on GitHub.

  • [Discord][discord]
  • [Stack Overflow][stackoverflow]

Contributors

Thanks goes to these people ([emoji key][emojis]):

t

Kent C. Dodds

💻 📖 🚇 ⚠️

Ryan Castner

📖

Daniel Sandiego

💻

Paweł Mikołajczyk

💻

Alejandro Ñáñez Ortiz

📖

Matt Parrish

🐛 💻 📖 ⚠️

Justin Hall

📦

Anto Aravinth

💻 ⚠️ 📖

Jonah Moses

📖

Łukasz Gandecki

💻 ⚠️ 📖

Ivan Babak

🐛 🤔

Jesse Day

💻

Issues· 82 open

View all issuesOpen on GitHub
  • #1209

    Support for React Server Components

    enhancementneeds investigationUpdated Jul 30, 2026
  • #1466

    bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict mode

    Updated Jul 29, 2026
  • #1413

    React act() warning persists with latest versions (React 18.3.1 + RTL 16.3.0 + Vitest 2.1.0)

    Updated Jul 23, 2026
  • #1421

    On jest, `afterEach(cleanup)` is too late to unmount a component using `useSyncExternalStore` with asynchronous updating

    Updated Jun 10, 2026
  • #1419

    Flaky Tests with user.type (missing characters)

    Updated Jun 10, 2026
  • #1415

    React 19 - Objects are not valid as a React child

    Updated Jun 10, 2026
  • #1405

    Provide example of how to test <details><summary>...

    Updated Jun 10, 2026
  • #1348

    Having `afterEach(cleanup)` in the test setup file breaks exported `screen` module on Bun test

    Updated Jun 10, 2026
  • #1417

    Unable to getByRole figure with accessible name / description in figcaption

    Updated Jun 10, 2026
  • #1407

    Support for React.cache function

    Updated Jun 10, 2026

> Tags

JavaScriptjavascriptreactjstesting

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架