Code Reference

React Testing Library

Jest · Reference cheat sheet

React Testing Library

Jest · Reference cheat sheet


📋 Overview

Testing Library tests React the way users interact: roles, labels, text — not implementation details. Pair with Jest + jsdom and @testing-library/jest-dom.

🔧 Core concepts

APIRole
renderMount component
screenQueries on document
userEventRealistic interactions
waitFor / findBy*Async UI
withinScope queries

Prefer getByRolegetByLabelTextgetByTextgetByTestId.

💡 Examples

Install:

npm i -D @testing-library/react @testing-library/jest-dom @testing-library/user-event jest-environment-jsdom
// jest.setup.js
import '@testing-library/jest-dom';

Component test:

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

test('submits email', async () => {
  const user = userEvent.setup();
  const onSubmit = jest.fn();
  render(<Login onSubmit={onSubmit} />);

  await user.type(screen.getByLabelText(/email/i), 'ada@ex.com');
  await user.click(screen.getByRole('button', { name: /sign in/i }));

  expect(onSubmit).toHaveBeenCalledWith({ email: 'ada@ex.com' });
});

Async:

expect(await screen.findByText(/welcome/i)).toBeInTheDocument();
await waitFor(() => expect(mock).toHaveBeenCalled());

⚠️ Pitfalls

  • Querying by className / enzyme-style selectors.
  • Forgetting userEvent.setup() with fake timers.
  • Not wrapping state updates that warn about act — usually fixed by findBy/await user.
  • Snapshotting entire container.innerHTML instead of behavior asserts.
  • Using getBy* for elements that appear asynchronously — use findBy*.

On this page