You've written tests in both earlier courses. Today is about which tests to
write. A good test suite does two things: it fails when the app breaks, and it
keeps passing while you restructure code that still works. A test that fails
after a harmless refactor is a cost, not a safety net.
The rule that gets you both: test what users and other code can observe,
not how it's done inside.
Four levels
Level
What it checks
Example from today
A plain function
Inputs to outputs, no React
A reducer, a selector, a formatter
A hook
What a hook returns as its inputs change
useDebouncedValue, useTopicSearch
A component
What a person sees and does
TopicSearch: type, see results
The app
Several parts working together
The whole page in a browser
Lower levels are fast and pinpoint the problem; higher levels catch the bugs
that live between parts. Most of your tests should be component tests that
use the screen like a person, plus focused tests for logic that's tricky on its
own, like timing and races.
The Topic Finder
Today's app searches a pretend server as you type. It waits until you stop
typing for 300 ms, then the server answers 400 ms later. A search with an !
in it fails, so you can see the error state.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Type sel, then sel!, then press Try again.
There's a lot to get right in useTopicSearch: the debounce, the loading and
error states, retrying, and ignoring a slow answer to an old search. Each of
those deserves a test:
The rest of today runs on your own computer. Press Download project under
the lab, unzip it, and open a terminal in the topic-finder folder. You need
Node.js 22.12 or later. Then:
import { defineConfig } from "vite";import react from "@vitejs/plugin-react";export default defineConfig({ plugins: [react()], test: { environment: "jsdom", globals: true, },});
npm test now runs Vitest in watch mode: it reruns the tests as you save.
Exercise
Sort the tests
For each test, pick the level it belongs at, and say whether it would survive
renaming status to phase inside useTopicSearch.
"Typing sel shows 1 result."
"After a search, the hook's state object is { status: "done", ... }."
"The Try again button is inside an element with role="alert"."
"A slow answer to an old search never replaces a newer one."
Hint 1: the idea
A test should depend on the contract of the thing it tests. Internal names and public return fields are different contracts.
Hint 2: a first step
Ask what each test reads: the screen, or the hook's return value? And is the
name status part of what it checks?
Show one solution
Component. It survives: it reads only the screen.
Hook. It breaks: status is the hook's public return value, so renaming it
is a real API change, and this test should fail. That's fine, as long as
tests don't reach into names that are private.
Component. It survives, and it checks something users rely on: screen
readers announce the alert.
Hook, because the timing is easiest to control there. It survives as long as
it checks results, not status.
Completion is saved in this browser only. It does not measure mastery.