ReactEasy
What is Testing?
Testing is writing automated checks that verify your code behaves as expected, so you can change it confidently. In React apps you typically combine unit tests for logic, component tests that render components and interact with them like a user (React Testing Library), and end-to-end tests that drive a real browser (Playwright, Cypress), plus static checks such as TypeScript and ESLint.
Types of tests
- Static analysis: TypeScript and ESLint catch whole classes of bugs without running code.
- Unit tests: pure functions, reducers, hooks, utilities. Fast and numerous.
- Component / integration tests: render components with Testing Library, simulate typing and clicking, mock network calls (MSW), assert on what the user sees.
- End-to-end (E2E): run the real app in a browser and test full flows (sign up, checkout).
- Visual regression: compare screenshots (Playwright, Chromatic).
- Accessibility tests:
jest-axe/vitest-axe, Playwright with axe.
Tools in 2026
- Test runners: Vitest (fast, Vite-native, very common for React), Jest (version 30, still widely used).
- React Testing Library (
@testing-library/react16) withuser-event. - MSW for API mocking.
- Playwright for E2E and component testing; Cypress as an alternative.
react-test-rendereris deprecated; use Testing Library.
Principles
- Test behavior, not implementation details ("The more your tests resemble the way your software is used, the more confidence they can give you").
- Query by role and label, which also checks accessibility.
- Keep tests independent and deterministic.
- Put most effort where bugs are expensive: core flows and business logic.
Code example
tsx
1// counter.tsx2import { useState } from "react";3export function Counter({ initial = 0 }: { initial?: number }) {4 const [count, setCount] = useState(initial);5 return <button onClick={() => setCount((c) => c + 1)}>Count: {count}</button>;6}78// counter.test.tsx (Vitest + React Testing Library)9import { describe, expect, it } from "vitest";10import { render, screen } from "@testing-library/react";11import userEvent from "@testing-library/user-event";1213describe("Counter", () => {14 it("increments when clicked", async () => {15 const user = userEvent.setup();16 render(<Counter initial={2} />);1718 const button = screen.getByRole("button", { name: /count: 2/i });19 await user.click(button);2021 expect(screen.getByRole("button", { name: /count: 3/i })).toBeInTheDocument();22 });23});
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.