What to Test in a Frontend, and at Which Level
Map regressions to the level that can catch them — unit, component or end-to-end — and learn what makes a test flaky and how to diagnose it instead of masking it.
Contentsand lesson path
On this page
- Why it matters
- The three levels and what each can see
- Choosing the level: cost, speed and confidence
- When tests lie: flakiness and how to read it
- Check your understanding
- Apply it
- Review prompt
Lessons of Component & End-to-End Testing
- What to Test in a Frontend, and at Which Level Concept · 20 min
- Component Tests That Read Like User Stories Worked example · 25 min
- Mock the Network, Not Your Code Worked example · 20 min
- End-to-End Journeys with Playwright Worked example · 25 min
- Lab: Test a Checkout Flow at Every Level Lab · 30 min
- Check: Fix the Flaky and the Brittle Knowledge check · 8 questions
What you will learn
- Choose a balanced mix of unit, component and end-to-end tests for a feature, and diagnose flaky tests using traces and retry data.
Prerequisites: React Components & State
01. Why it matters
Frontend code fails in ways that pure logic does not. A price can format correctly in every unit test yet be invisible on screen because a wrapper sets display: none. A form can work with a mouse but trap keyboard users. A deployed site can call an API path that no longer exists, even though every part of it passed in isolation. Tests catch these failures before users do — but only if each check sits at a level that can actually see the problem.
This lesson gives you a map of the three levels used to test frontends — unit, component and end-to-end — and a rule for deciding where each check belongs. It ends with the failure mode that wastes more team time than any other: the flaky test.
02. The three levels and what each can see
A unit test runs a small piece of logic on its own — a price formatter, a discount rule, a date helper — with no browser and no DOM. It is the fastest feedback you can buy, measured in milliseconds, and it pinpoints failures exactly. Its blindness is the price of that speed: it cannot tell you anything about rendering, events or accessibility, because none of those exist in its world.
A component test renders one component, or a small tree, in a simulated browser such as jsdom, fires events at it with tools that imitate a real user, and asserts what becomes visible. The network is mocked. This level sees the behaviour users recognise — labels, roles, messages — while staying fast enough to run after every save.
An end-to-end (E2E) test drives a real browser against the built application: the compiled bundle, the real routing, the real network stack and the backing services. It is the only level that can prove the deployed system works as a whole, and it is the slowest and the most sensitive to infrastructure.
Level Runs in Speed Can see Cannot see
Unit Node ms pure logic any rendering
Component jsdom tens of ms rendered output and layout, real network
events, mocked network
E2E real browser seconds the whole built system what it never asserts03. Choosing the level: cost, speed and confidence
A regression is a change that breaks something users rely on. The rule for choosing a level is: put each check at the lowest level that can catch that regression.
Pure logic belongs in unit tests. A rounding bug does not need a browser to reproduce, and a unit test that pins formatPrice(9849) to £98.49 fails in milliseconds with an exact message.
Anything a user can see or do inside one component belongs in a component test: the message that appears after a failed request, the list that renders while typing, the button that disables during submission.
Only cross-cutting behaviour needs E2E: the journey from signed-in user to paid order, across routing, the real build and the real network. Each E2E test is expensive to write and to keep green, so spend that budget on the journeys whose failure would cost the most — checkout, sign-up, the core workflow your product exists for.
The result is a shape with many unit tests, a healthy layer of component tests, and a few E2E journeys. Frontend code leans harder on the component layer than the classic test pyramid suggests, because most frontend risk lives in rendered behaviour rather than in pure functions.
The payoff is feedback time. A component suite finishes in seconds and runs on every save; an E2E suite takes minutes and runs on every push. When something breaks, the failure that a lower level catches names the fault directly, while an E2E failure only tells you that somewhere in a whole journey, something went wrong. That is also why moving checks down is the first lever when a suite feels slow — not buying more machines. The guardrails lesson in the tooling skill shows where these suites plug into a project workflow.
For how the suites fit into day-to-day project work, see the guardrails lesson.
04. When tests lie: flakiness and how to read it
A test is flaky when the same code passes on one run and fails on the next. Flakiness is among the most damaging properties a test suite can have, because it destroys the meaning of red. Once a team has seen enough unexplained failures, every red build looks like noise, and a genuine regression hides among the flakes and ships to production.
Flakiness has a short list of causes: tests that depend on timing (asserting before the interface has finished updating), tests that share mutable state (two journeys editing the same account while running in parallel), tests that depend on the order they run in, and tests that reach real services whose responses vary.
The tools in this skill attack these causes directly. Playwright's web-first assertions retry until a condition holds or a timeout expires, which removes sleeps. Mocking the network at its boundary makes responses deterministic instead of dependent on a live backend (a later lesson shows how). Fixtures give each test its own data. When a failure does appear, Playwright's trace viewer records every action, DOM snapshot, network request and console message of a run, which usually turns a mystery into a diagnosis: compare the failing run's trace with a passing one, and look for the race.
Retries exist, and a green retry is better than a red build — but a retry is a painkiller, not a cure. Find the cause. One practical habit: after fixing a flaky test, run it many times in a row (Playwright has --repeat-each) before you trust it again.
05. Check your understanding
06. Apply it
Take the search or checkout feature your team owns — the same one the practice task for this skill uses. List five regressions you are afraid of, from a rounding error in a discount rule to a checkout button that stops working after a redesign. For each one, write down the lowest level that can catch it.
Then audit yourself: any planned E2E test whose failure a component test would catch just as well should move down, and any pure function currently tested through rendered output should get a unit test. Keep the E2E budget for journeys.
07. Review prompt
Without looking back: name the three levels, what each one runs in, and one thing each cannot see. Say where a validation rule for a discount code belongs, and where the complete sign-up-and-pay journey belongs. Finally, define a flaky test, name two of its causes, and name two tool features that prevent them.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.