Skip to content
ClapelloNotes by Barath
ReactCh 13 · 1/6
Beginner4 min readUpdated

React / 70

Why test, and which tests to write

TL;DR

Automated tests re-check your app in seconds after every change. Unit, integration and end-to-end tests each catch different bugs.

Manual vs automated

Manual testingAutomated testing
HowYou click around the appCode checks the app for you
SpeedMinutes, every timeSeconds, every time
Repeatable?You forget stepsSame steps, every run
When it runsWhen you rememberOn every save, and before every merge

QA teams do lots of kinds of testing. This chapter is about developer tests: small programs, written next to your code, that check it still does what it should.

Three kinds of tests

TypeWhat it checksTasty Bites exampleTool
UnitOne function or one small component, alonecartTotal() adds price × qty correctlyVitest or Jest
IntegrationSeveral pieces working togetherType "burger" in search, press Search, see only burgersVitest/Jest + React Testing Library
End-to-end (E2E)The real app in a real browser, start to finishOpen the site, add a dosa, check outPlaywright

There's also static checking: TypeScript and ESLint catch typos and wrong types before any test runs.

How many of each?

  1. End-to-end

    a real browser clicks through the whole app

    Playwright

  2. Integration

    several components working together

    Vitest / Jest + Testing Library

  3. Unit

    one function or one small component

    Vitest / Jest

  4. Static

    typos and type errors, before running

    TypeScript · ESLint

The testing trophy: the widest layer is where most of your tests should go. Higher layers are slower and costlier, but closer to what a real user does.

The classic advice is a pyramid: lots of unit tests, fewer integration tests, very few E2E tests. For UI code, many React developers prefer the testing trophy above: make integration tests the biggest group, because users never use one function alone. A test that renders the search box, types, clicks and checks the results catches far more real bugs than ten tests of tiny helpers.

Test behaviour, not implementation

React Testing Library is built on one idea: the more your tests resemble the way your software is used, the more confidence they can give you.

Don't

Testing implementation details

"After clicking, the isOpen state is true." Rename the variable and the test breaks, even though the app still works.

Do

Testing what the user sees

"After clicking Menu, a link named Cart is visible." Refactor freely: this only fails if the user would notice.

A taste of end-to-end

E2E tests run your real app in a real browser. Playwright is today's go-to tool (set it up with npm init playwright@latest):

e2e/cart.spec.js
import { test, expect } from "@playwright/test";
 
test("a user can add a dish to the cart", async ({ page }) => {
  await page.goto("http://localhost:5173/");
  await page.getByRole("button", { name: "Add Masala Dosa" }).click();
  await expect(page.getByRole("link", { name: "Cart (1)" })).toBeVisible();
});

They're the most realistic, but also the slowest, so keep them for the few journeys that must never break (sign-up, checkout). The rest of this chapter is about unit and integration tests.

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.