Kayavak / The Web Frontend / FE·26 Frontend Testing / Deeper
Deeper · The Web Frontend
Frontend Testing
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Browser automation began with Selenium (Jason Huggins at ThoughtWorks, 2004), which drove a real browser to click and type like a person; WebDriver later became a W3C standard (2018). For years these tests were slow and flaky, failing on timing rather than bugs, so teams wrote few of them. Mike Cohn's test pyramid (2009) put many fast unit tests at the bottom and few end-to-end tests at the top.
Frontends challenged the pyramid: most bugs live where components meet the DOM, data and each other, which unit tests of pure functions don't reach. Testing Library (Kent C. Dodds, 2018) made component tests query the page the way users do (by role, label and text) instead of by implementation details, and Dodds proposed the testing trophy, weighted towards integration tests. Cypress (2017) and Playwright (Microsoft, 2020) made end-to-end tests faster and much less flaky with automatic waiting. Mutation testing, proposed by Richard Lipton in 1971, checks the tests themselves by planting bugs and seeing whether any test fails.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
A test is a claim about behaviour that runs. Its value is the confidence it gives you to change the code; its cost is the time to run it, the time to maintain it, and every false alarm it raises.
| Question | Answer |
|---|---|
| What is it? | Automated checks of frontend behaviour at several levels, from functions to the whole app in a browser |
| Problem it solves | Changing UI code without breaking what users rely on |
| Parts | Unit, component/integration and end-to-end tests; test doubles; visual and accessibility checks; mutation testing |
| How it works | Render or load the UI, act as a user would, assert on what the user would see |
| Assumptions | Tests observe behaviour through public interfaces |
| When they change | Tests tied to implementation details break on every refactor and pass when behaviour breaks |
| Where it fails | Flaky tests teach teams to ignore failures; mocks that differ from the real thing test the mock |
| Related ideas | Testing strategy (Testing); accessibility queries double as accessibility checks (ch20) |
What if?
What if your tests had 100% coverage?
Reason it through, then open
Every line would have run during a test, which says nothing about whether any test checked its result. A test that
renders a component and asserts nothing covers every line. Mutation testing measures what coverage can't: change <
to <= and see if a test fails. If none does, that line is covered but untested.
What if end-to-end tests waited a fixed two seconds before each check?
Reason it through, then open
On a fast machine they'd waste time; on a slow CI runner they'd sometimes check too early and fail: flaky by design. Auto-waiting (retrying an assertion until it holds or a timeout passes) ties the test to the condition rather than the clock, which is why modern tools made end-to-end tests far more reliable.
Where the boundary is
| Aspect | Code coverage | Mutation score |
|---|---|---|
| Measures | Which lines ran during tests | Which planted bugs tests caught |
| Says about test quality | Little: running isn't checking | Directly: a surviving mutant is an untested behaviour |
| Cost | Cheap | Expensive: runs the suite once per mutant |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Mutation testing proposed by Lipton in 1971 | Historical | Later published with DeMillo and Sayward (1978) |
| Coverage correlates weakly with test effectiveness once size is controlled | Measured | Inozemtseva and Holmes, ICSE 2014 |
| Integration tests give the best confidence per cost for UIs | Model | The testing-trophy argument; reasonable, not proven in general |
| Flaky tests erode trust in a suite | Measured | Studies of large codebases at Google and elsewhere |
Still open
How much to trust AI-generated tests is new territory: they raise coverage quickly, but often assert what the code does rather than what it should do. Visual-regression testing's signal-to-noise ratio is still a practical problem.
What would change your mind? If unit tests alone caught most UI regressions in real projects, the case for component and end-to-end tests would weaken. Bug reports from production usually say otherwise.
Recall
Why doesn't 100% coverage mean good tests?
Coverage shows lines ran, not that any test checked their results.
What does mutation testing measure?
Whether tests catch deliberately planted bugs; surviving mutants reveal untested behaviour.
Why query by role and label in component tests?
It tests what users see and survives refactors; it also exercises accessibility.
What makes end-to-end tests less flaky?
Waiting for conditions (auto-waiting) rather than fixed delays.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.