React / 70
Why test, and which tests to write
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 testing | Automated testing | |
|---|---|---|
| How | You click around the app | Code checks the app for you |
| Speed | Minutes, every time | Seconds, every time |
| Repeatable? | You forget steps | Same steps, every run |
| When it runs | When you remember | On 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
| Type | What it checks | Tasty Bites example | Tool |
|---|---|---|---|
| Unit | One function or one small component, alone | cartTotal() adds price × qty correctly | Vitest or Jest |
| Integration | Several pieces working together | Type "burger" in search, press Search, see only burgers | Vitest/Jest + React Testing Library |
| End-to-end (E2E) | The real app in a real browser, start to finish | Open the site, add a dosa, check out | Playwright |
There's also static checking: TypeScript and ESLint catch typos and wrong types before any test runs.
How many of each?
End-to-end
a real browser clicks through the whole app
Playwright
Integration
several components working together
Vitest / Jest + Testing Library
Unit
one function or one small component
Vitest / Jest
Static
typos and type errors, before running
TypeScript · ESLint
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):
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.
