🧪
Testing10M+/wkMIT

@testing-library/react

Testing Library is a family of packages that helps you test UI components in a way that resembles how users actually interact with your application. The core ph

Installation

npm
npm install -D @testing-library/react @testing-library/jest-dom
yarn
yarn add -D @testing-library/react @testing-library/jest-dom
pnpm
pnpm add -D @testing-library/react @testing-library/jest-dom

Import

ESM
import { render, screen } from '@testing-library/react';

Quick Example

usage
import { render, screen } from '@testing-library/react';

test('renders greeting', () => {
  render(<Greeting name="World" />);
  expect(screen.getByText('Hello, World')).toBeInTheDocument();
  expect(screen.getByRole('heading')).toHaveTextContent('Hello');
});

About @testing-library/react

Testing Library is a family of packages that helps you test UI components in a way that resembles how users actually interact with your application. The core philosophy is that tests should interact with rendered output rather than implementation details, using queries like getByText, getByRole, getByLabelText, and getByPlaceholderText that mirror how users and assistive technologies find elements. This approach produces tests that are more resilient to refactoring and better at catching accessibility issues. The React variant (@testing-library/react) provides render() for mounting components and screen for querying the rendered output, along with fireEvent and userEvent for simulating user interactions. The library integrates with Jest or Vitest and includes async utilities like waitFor and findBy queries for testing asynchronous state changes. Testing Library is available for React, Vue, Angular, Svelte, Preact, and vanilla DOM, with a consistent API across frameworks. The guiding principle — 'The more your tests resemble the way your software is used, the more confidence they can give you' — has made it the dominant component testing approach in the React ecosystem.

Quick Facts

Package@testing-library/react
CategoryTesting
Weekly Downloads10M+
LicenseMIT
Installnpm install -D @testing-library/react @testing-library/jest-dom

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.