When visual tests are worth it
Why This Matters
Visual tests are cheap to write and expensive to keep. Every intended design change means new baselines and a review of every image. A suite with a hundred screenshots of pages that change weekly spends more time being updated than finding bugs, and soon nobody looks at the diffs any more. Choose where the layout is the risk.
Good candidates
| Candidate | Why |
|---|---|
| Shared components (buttons, form fields, cards) | Used everywhere; one CSS change breaks many pages at once |
| States that are easy to get wrong | Error messages, empty states, a disabled “Out of Stock” button |
| Critical screens | Checkout, login, anything where a broken layout costs money or trust |
| Layouts at different sizes | A mobile viewport where elements wrap or overlap |
The five tests in the example repository follow this: the login form in its normal and error state, one product card, the empty shop and one full page.
Poor candidates
| Candidate | Why not |
|---|---|
| Content-heavy pages | Every new article or product changes the screenshot |
| Pages with live data | Prices, stock and dates need so many masks that little is left |
| Everything, “to be safe” | Hundreds of baselines, each one a reason for a red build after a harmless change |
| Behaviour | “The cart shows 2 items” is a functional assertion; a screenshot of it fails on a font change and says nothing about the number |
Functional first, visual on top
A visual test complements functional tests; it does not replace them. Compare:
await expect(page.getByTestId("cart-total")).toHaveText("$72.59"); // what the page says
await expect(page.getByTestId("checkout-modal")).toHaveScreenshot(...); // how it looks
The first fails with Expected “$72.59”, Received “$72.99”: a clear message. A screenshot of the same mistake fails with a diff image you have to study. Use the functional assertion for facts, and the screenshot for layout.
Reviewing a failure
When a visual test fails, open the HTML report (npm run report). Each failed comparison has three views:
- Expected: the baseline.
- Actual: what the page looks like now.
- Diff: the changed pixels in red. The slider compares the two directly.
Then decide, exactly as for the wrong expectation in module 3: is the change intended (update the baseline, review it, commit it) or is it a bug (report it, keep the baseline)?
Exercise
For each part of practiceautomatedtesting.com below, decide whether a visual test is worth it, and if so, which element and which masks:
- The product grid with the real products
- The login page’s “Practice accounts” table
- The cart button in the header (“2 items $84.98”)
- The checkout’s billing form, after clicking Place Order with empty fields
Show suggested answer
- Not the real grid. Products, photos and prices change, so it needs many masks and still breaks when a product is added. One card (as in the repository) or the stubbed empty state covers the layout risk.
- Probably not on its own. It is static text that a functional assertion checks more clearly. It is covered anyway by the whole-page screenshot.
- No. The number and total are facts:
toContainText("2 items")says more than a picture. The button’s style is covered by a component test elsewhere. - Yes. Error states under every field are where layouts break: long messages wrap, fields shift. Take an element screenshot of the billing form after the real API’s validation errors appear (module 5), with nothing to mask, because the messages are fixed.