Repolode
testing-library

testing-library/react-testing-library

Lightweight React DOM testing utilities that query and interact with components the way users do.

Stars
19,672
Forks
1,171
Open issues
84
Last commit
2 months ago

What is it?

React Testing Library provides utility functions built on top of react-dom and react-dom/test-utils for testing React components. Its guiding idea is that tests should resemble how the software is used, so the library focuses on DOM nodes rather than component instances and avoids exposing implementation details.

The library offers render, screen queries such as getByRole, getByLabelText and getByText, and fireEvent for simulating user interaction. Queries with get, query and find prefixes cover the common cases of expecting an element, asserting its absence, or waiting for it to appear. Starting from version 16, it requires @testing-library/dom as a peer dependency, and versions 13 and later require React 18.

It is commonly used with Jest or other test runners and pairs well with @testing-library/jest-dom for DOM assertions and Mock Service Worker for mocking API calls. Separate React Hooks Testing Library exists for reusable custom hooks.

Key features

  • 01Queries elements by accessible roles, labels and text, mirroring how users find them.
  • 02Provides render and fireEvent helpers for mounting components and simulating interactions.
  • 03Includes async find and waitFor utilities for asynchronous UI updates.
  • 04Works with Jest and other test runners on top of react-dom.
  • 05Pairs with jest-dom matchers for readable DOM assertions.

Use cases

  • Writing unit or integration tests for React components that stay stable through refactors.
  • Testing forms, user flows and conditional rendering in React applications.
  • Verifying asynchronous UI behavior such as loading states and fetched data.

Quick start

  1. 1

    Install React Testing Library and DOM testing utilities as dev dependencies with npm

    npm install --save-dev @testing-library/react @testing-library/dom
  2. 2

    Or install them with yarn instead

    yarn add --dev @testing-library/react @testing-library/dom
  3. 3

    Write a test that renders a component and queries it the way users do, using render and screen from the library

Things to know

  • Starting from version 16 it requires installing @testing-library/dom separately.
  • Versions 13 and later require React 18; older React projects must use version 12.

Topics

FAQ

What is react-testing-library?
Lightweight React DOM testing utilities that query and interact with components the way users do. React Testing Library provides utility functions built on top of react-dom and react-dom/test-utils for testing React components. Its guiding idea is that tests should resemble how the software is used, so the library focuses on DOM nodes rather than component instances and avoids exposing implementation details.
Is react-testing-library free to use? What license does it have?
react-testing-library is open source under the MIT license. Check the license terms before using it in commercial products.
Is react-testing-library still maintained?
Updated within 6 months. The last commit to react-testing-library was on Aug 27, 2026.
What can I use react-testing-library for?
Writing unit or integration tests for React components that stay stable through refactors. Testing forms, user flows and conditional rendering in React applications. Verifying asynchronous UI behavior such as loading states and fetched data.
How do I install react-testing-library?
Install React Testing Library and DOM testing utilities as dev dependencies with npm: npm install --save-dev @testing-library/react @testing-library/dom Or install them with yarn instead: yarn add --dev @testing-library/react @testing-library/dom Write a test that renders a component and queries it the way users do, using render and screen from the library
Where can I get react-testing-library?
The source code and installation instructions are on GitHub: https://github.com/testing-library/react-testing-library

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects