Back to Curriculum

Testing React Components

📚 Lesson 12 of 18 ⏱️ 50 min

Testing React Components

50 min

Testing is crucial for maintaining code quality in React applications.

React Testing Library provides utilities for testing React components in a way that resembles how users interact with your app.

Jest is commonly used as the test runner and assertion library.

React Testing Library encourages testing behavior rather than implementation details. This makes tests more resilient to refactoring and closer to how users interact with your application.

The testing philosophy focuses on finding elements by their accessible roles, labels, or text content rather than by implementation details like class names or component structure.

Writing good tests helps catch bugs early, documents expected behavior, and provides confidence when refactoring code.

Key Concepts

  • React Testing Library tests user behavior, not implementation details.
  • Jest is the test runner and assertion library for React tests.
  • Tests should resemble how users interact with components.
  • Use queries like getByRole, getByText, and getByLabelText.
  • fireEvent and userEvent simulate user interactions.

Learning Objectives

Master

  • Writing tests with React Testing Library
  • Using Jest for assertions and test organization
  • Testing user interactions with fireEvent and userEvent
  • Querying elements in accessible ways

Develop

  • Test-driven development thinking
  • Writing maintainable and resilient tests
  • Understanding testing best practices

Tips

  • Test behavior, not implementation details.
  • Use getByRole queries for better accessibility testing.
  • Prefer userEvent over fireEvent for more realistic interactions.
  • Keep tests focused and test one thing at a time.

Common Pitfalls

  • Testing implementation details instead of user behavior.
  • Using data-testid as the primary query method (use accessible queries first).
  • Writing tests that are too coupled to component structure.
  • Not testing edge cases and error scenarios.

Summary

  • React Testing Library encourages testing user behavior.
  • Jest provides the testing framework and assertions.
  • Accessible queries make tests more resilient.
  • Good tests provide confidence for refactoring.

Exercise

Write a test for a simple counter component.

import { render, screen, fireEvent } from '@testing-library/react';
import Counter from './Counter';

test('renders counter with initial count of 0', () => {
  render(<Counter />);
  expect(screen.getByText('You clicked 0 times')).toBeInTheDocument();
});

test('increments count when button is clicked', () => {
  render(<Counter />);
  const button = screen.getByText('Click me');
  fireEvent.click(button);
  expect(screen.getByText('You clicked 1 times')).toBeInTheDocument();
});

Exercise Tips

  • Use getByRole('button') instead of getByText for better accessibility.
  • Add tests for edge cases like multiple clicks.
  • Test both the initial state and state changes.
  • Consider using userEvent.click() for more realistic interactions.

Code Editor

Output