Frontend testing strategy

Testing Library philosophy (query by role, not test-id), mocking network with MSW, a thin Playwright e2e layer for critical flows. Test behavior, not implementation.

About 28 min read3-question knowledge check

The mental model

A frontend test asks one question: if I ship this, will the user still be able to do the thing? Everything else — coverage numbers, mocking style, which runner — is in service of answering that fast and honestly.

The analogy that fixes most bad testing: a restaurant health inspection. A useless inspection checks that the fridge is a particular brand and the knives are arranged a certain way — details that can change without affecting a single meal. A good inspection checks that the food comes out safe and correct. Testing *implementation* (this component calls setState twice, this function is named handleClick) is inspecting the brand of the fridge. Testing behaviour (the user types, sees results, clicks one, and it is selected) is inspecting the meal. The first kind breaks every refactor and catches nothing; the second breaks only when a user-visible contract breaks, which is exactly when you want it to.

Why this exists. Frontends change constantly — a redesign moves every element, a state refactor renames every variable — while the behaviour stays the same. A test suite coupled to implementation turns every refactor into a day of updating tests that were green for the wrong reasons, and teams respond by not refactoring and not trusting the suite. The Testing Library philosophy — "the more your tests resemble the way your software is used, the more confidence they give you" — exists to break that cycle: query the DOM the way a user perceives it (by role, by label, by text), assert on what the user experiences, and the suite survives refactors while catching real breaks.

The organising idea is the testing trophy (Kent C. Dodds' refinement of the pyramid): a broad base of static checks (TypeScript, ESLint) that catch typos and type errors for free, the largest *investment* in integration tests (a component tree with its real collaborators, network mocked at the boundary), fewer unit tests for genuinely complex pure logic, and a thin top layer of end-to-end tests for the few flows that would be a disaster to break. Integration is the sweet spot because it maximises confidence per test: close enough to real usage to catch integration bugs, cheap enough to run thousands of times.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision