@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 install -D @testing-library/react @testing-library/jest-dom
yarn add -D @testing-library/react @testing-library/jest-dom
pnpm add -D @testing-library/react @testing-library/jest-dom
Import
import { render, screen } from '@testing-library/react';Quick Example
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 |
| Category | Testing |
| Weekly Downloads | 10M+ |
| License | MIT |
| Install | npm install -D @testing-library/react @testing-library/jest-dom |
Related Packages
Jest is a delightful JavaScript testing framework with a focus on simplicity, created by Facebook. I…
Vitest is a blazing-fast unit testing framework powered by Vite, designed as a modern alternative to…
React is the most popular JavaScript library for building user interfaces, developed and maintained …
Cypress is a next-generation front-end testing tool built for the modern web, providing a complete e…
Mock Service Worker (MSW) is an API mocking library that uses the Service Worker API to intercept re…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.