Testing Library/react Jun 2026

import render, screen from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import ToggleButton from './ToggleButton';

The guiding principle of React Testing Library is: . testing library/react

const button = screen.getByRole('button', name: /off/i ); await user.click(button); is a family of testing utilities that encourage

test('displays the correct greeting', () => render(<Greeting name="Alice" />); const heading = screen.getByText(/hello, alice!/i); expect(heading).toBeInTheDocument(); ); screen from '@testing-library/react'

test('calls onClick handler', () => const onClick = jest.fn(); const getByText = render(<Button onClick=onClick />); const button = getByText('Click me!'); fireEvent.click(button); expect(onClick).toHaveBeenCalledTimes(1); );

The Testing Library/React is a testing library developed by the React team, designed to make it easy to test React components in isolation. It's built on top of Jest, a popular testing framework, and provides a set of APIs that allow you to render and interact with your React components in a test environment.

is a family of testing utilities that encourage testing React components the way a user would interact with them. The specific package @testing-library/react integrates with Jest and other test runners to query and interact with the DOM.