Originally published with interactive examples at lukiluki.me.
You change a seemingly harmless CSS class in the header and push the branch. Twenty minutes later, checkout is broken. Nobody manually tried the payment flow, and automated checks did not cover it. How do you catch important regressions without clicking through the whole application after every change?
Frontend testing combines fast checks of isolated logic, component tests of user interactions and browser tests of critical journeys. Vitest, React Testing Library and Playwright address different parts of that strategy. Choose tests according to risk and the behavior they verify. A passing suite provides evidence for the cases it covers; it cannot guarantee correctness, accessibility or a fixed CI runtime for every project.
Merksatz (Key takeaway):
Test observable behavior from the user's perspective. Internal refactoring should not break a test unless the behavior it protects has changed.
The three layers of a frontend test strategy
The test pyramid is a planning model, rather than a mandatory ratio. Fast isolated checks give frequent feedback; broader tests check how pieces work together. The Testing Library guiding principles favor interacting with the rendered DOM instead of component instances.
| Layer | Typical tool | What to verify | What it does not establish |
|---|---|---|---|
| Unit | Vitest | Calculations, transformations and isolated business rules | The complete interface or real browser behavior |
| Component integration | React Testing Library with a suitable test runner | Labels, visible states and interactions between components | Full browser layout or real backend integration when simulated |
| End to end | Playwright | Critical flows such as sign-in, basket and checkout in a browser | Every input, failure or supported device from one happy-path test |
A simulated DOM such as jsdom is useful for many component tests, but it is not a full rendering engine. Layout, browser APIs and integrations may need real-browser checks. Keep that boundary visible when interpreting a green test run.
Architectural choices that make tests useful
Use accessible roles and labels to locate controls when they reflect how people use the interface. Query a “Save” button by role and name rather than a styling class. This aligns tests with useful semantics, but finding a role alone does not audit contrast, keyboard behavior or all accessibility requirements.
Mock external services where you need controlled responses, while keeping the application behavior under test real. A network mock can reproduce a timeout or rejected payment. If the entire interaction is replaced by mocks, the test may only confirm its own setup.
Flaky tests (Tests that pass and fail without an intentional change to the behavior under test, often because timing, shared state or external dependencies are uncontrolled.) erode confidence. Investigate races, shared data, animation and environmental differences. A temporary quarantine should have an owner and a repair plan, rather than becoming a permanent ignored failure.
Wait for behavior, rather than sleeping
Configure a Playwright test project and a test environment before applying this pattern. Replace the route and accessible labels with those in your application. It assumes a sign-in form and a controlled test account, not a live production login.
import { test, expect } from "@playwright/test";
test("shows a helpful error for missing sign-in details", async ({ page }) => {
await page.goto("/sign-in");
await page.getByRole("button", { name: "Sign in", exact: true }).click();
await expect(page.getByRole("alert")).toContainText("Enter your email");
});
This is an illustrative assertion for an application that renders that alert. Adapt it when native form validation prevents submission or your product uses another accessible error pattern. The important mechanism is waiting for an expected visible state. Playwright recommends web-first assertions, which retry until the assertion succeeds or times out, rather than arbitrary fixed delays.
Keep feedback fast and failures explainable
Run focused tests locally and a useful set of checks in CI. Parallel execution can shorten feedback, but only if tests are isolated and the environment has capacity. A target such as a few minutes is a team budget to measure, rather than a guaranteed property of a particular tool.
For visual regression checks, control viewport, fonts, data and animations. Review the difference before accepting a new baseline. A changed screenshot can reveal a regression, but it cannot decide whether a design change was intended.
Prioritize critical paths and error cases. Branch coverage can highlight untested code, but a percentage does not prove that assertions are meaningful. For calculations with serious consequences, explicitly test boundary values, invalid input and domain rules.
Combine automated and manual accessibility checks
Role-based queries can reveal missing names or incorrect semantics. They complement keyboard and assistive-technology checks described in Accessibility; they do not replace them.
Frontend testing checklist
- [ ] Identify critical journeys and assign checks to the smallest useful test layer.
- [ ] Locate controls by meaningful roles, labels or visible text where appropriate.
- [ ] Wait for observable states and isolate test data instead of relying on fixed sleeps.
- [ ] Cover important failures and boundary conditions, then inspect what assertions actually verify.
- [ ] Measure CI duration and repair flaky tests before the team learns to ignore them.
Sources and next steps
- Testing Library: Guiding principles – tests centered on rendered behavior.
- Playwright: Best practices – isolation, locators and web-first assertions.
Git Workflows explains where review and automated checks fit into integration. Accessibility covers the usability checks that a test suite alone cannot establish.
Read the original guide & explore the interactive playground on *lukiluki.me*.
Top comments (0)