
Running Visual Tests in CI
Pipeline placement for the visual suite: an isolated stage, the per-PR default with honest downgrades, sharding, the artifact contract, and the in-CI baseline update flow.
TL;DR: Visual tests render real UI — whole pages or individual components — in real browsers and are structurally slower than the functional suite, so they belong in their own tagged stage rather than inlined into the default check. Run them per pull request by default — regressions are cheapest to judge next to the code that caused them — with merge-time, nightly, or path-filtered runs as honest downgrades. Shard for runtime, publish the expected/actual/diff triplet as retained artifacts linked from the check, and update baselines through a deliberate in-CI trigger that commits regenerated images back for review. Gate on confidence: block where coverage is stable, report-only while an axis is still shaking out, and treat permanent report-only as decay.
An isolated stage, not a slower everything
A visual test renders real UI — a whole page or an individual component — in a real browser engine and captures it. That is far slower than a unit assertion, and inlining it into the default check makes every push — including the ones that only touch a build script or a README — pay that cost.
The isolation pattern keeps the fast feedback path fast. Tag the visual tests, exclude them from the default run, and run them as a dedicated stage or workflow with its own container, its own cache, and its own status check. Developers waiting on lint and unit results are not also waiting on a browser to paint, say, forty pages.
# functional path: everything except the visual tag
npx playwright test --grep-invert @visual
# dedicated visual stage, in the reference container
npx playwright test --grep @visualThe dedicated stage also carries the determinism contract. It runs in the reference rendering environment by definition — the same container image, the same fonts, the same configuration every time — which a developer's local shell, with its own OS font rendering and its own browser build, only ever approximates. Baselines generated anywhere else may not match: Playwright's documentation warns that "browser rendering can vary based on the host OS, version, settings, hardware, power source (battery vs. power adapter), headless mode, and other factors" and, for consistent screenshots, advises running tests "in the same environment where the baseline screenshots were generated" (as of November 2026). So the stage is not just where visual tests run fastest; it is where they consistently run in the reference environment the baselines were made in. Tagging and grep-based inclusion are runner mechanics; the flags shown are Playwright's (as of November 2026, Playwright 1.65) and should be re-verified against current documentation.
When the stage runs
Per pull request is the default worth defending. A visual regression is cheapest to judge next to the code that caused it, with the author still in context and able to say whether the change was intended. Move the run later and that context evaporates.
The downgrades exist, and they should be described honestly rather than sold as optimizations. A merge-time or nightly run trades detection latency for pipeline cost, and its failure arrives orphaned — separated from the change that caused it, so someone has to bisect which of the last several merges is responsible before they can even start judging the diff. That reconstruction is pure overhead that a per-pull-request run largely avoids. It is acceptable for low-churn surfaces where merges are rare and the suspect list is short, and corrosive for shared ones where a nightly failure could belong to any of a dozen changes and ownership of it is unclear.
Path filtering is a middle way. Run the visual stage when rendering-relevant paths change — styles, components, design tokens, dependency manifests — and skip it for documentation-only diffs. The honest caveat is that a path filter encodes an assumption about what can affect rendering, and that assumption is occasionally wrong: a config change with no obvious visual surface can still move a layout, a dependency bump can change a rendering default, a feature flag can flip a component into a variant the filter never anticipated. A path filter should be treated as a best-effort optimization with a known failure mode, not a precise model of causality.
Whatever the trigger, it is a documented suite decision, not an accident of pipeline history. Someone should be able to read why the visual stage runs when it does.
Runtime engineering
Beyond the runner's own worker parallelism on one machine, sharding across machines is the next runtime lever. The suite splits across parallel runners, each executing a slice; each runner produces its own report, and the reports can be merged into one afterward (in Playwright, by using the blob reporter on CI and running a separate npx playwright merge-reports step, as of November 2026).
# one shard per runner; reports merged afterward
npx playwright test --grep @visual --shard=1/4Starting from a prebuilt image that already contains the browsers and their system dependencies, and caching the container layers, keeps stage startup from dominating a small suite, where installing browsers and building an image cold can otherwise cost more than the handful of tests that follow. Caching the browser binaries on their own is a different matter: Playwright's CI documentation says it is not recommended, because restoring the cache takes a time comparable to downloading the binaries and, especially under Linux, the operating system dependencies still need to be installed and are not cacheable; teams that still want to cache them between CI runs can, against a hash of the Playwright version (as of November 2026). On a suite of a few dozen captures, setup time can be a large share of the stage's runtime.
The matrix multiplies this stage's cost directly: every engine and viewport axis is another full pass. When runtime here starts to hurt, that pressure can send a team back to the selection and matrix decisions to ask whether every covered surface and every axis is still earning its place. That is the healthy direction for the pressure to push.
Artifacts and the judgment loop
A failed visual stage must publish the expected, actual, and diff images as retained artifacts, linked from the status check. A red check with no judgeable images invites re-runs rather than review — a reviewer can click "re-run" hoping it passes, because looking at the failure requires work the check did not make easy.
Retention is finite and asymmetric. Keep failure artifacts long enough to survive review latency — a few days past the typical time-to-review — and treat passing-run captures as disposable, since a test that passed leaves no failure to review.
The report is the reviewer's entry point. A runner HTML report or a pull-request comment summary that surfaces the triplet inline saves the reviewer from downloading a zip and opening three images by hand — friction that, multiplied across every diff, can decide whether a review happens or gets skipped. The judgment itself — intended versus regression — is the diff-review craft; this stage's job is to deliver the evidence in a form that makes judging it cheap enough that people actually do.
The in-CI update flow
The laptop round-trip works: pull the branch, enter the container, regenerate the baselines, commit, push. It does not scale past occasional updates, and it tempts the shortcut that breaks the determinism contract — regenerating outside the reference container, where the images can come out subtly different and the next CI run can fail on the mismatch.
The in-CI pattern removes the round-trip. A deliberate trigger — a label, a comment command, or a manual workflow dispatch — runs the update flag inside the reference environment and commits the regenerated baselines back to the branch.
# runs only on the deliberate trigger, inside the reference container
npx playwright test --grep @visual --update-snapshotsThe trigger must stay deliberate and scoped. A pipeline that auto-updates baselines on every failure is the blanket-update anti-pattern automated: it makes every regression self-approving, because the first failing run rewrites the reference to match the broken output and the second run passes. Updated baselines should land as reviewable diffs in the same pull request, feeding the approval convention rather than bypassing it — the person merging still sees exactly which reference images changed and can refuse the ones that should not have.
Block or report
The gate decision has two honest positions. A blocking visual stage stops regressions at merge, and makes every flake and every intended change a merge obstacle until someone clears it. A reporting stage preserves velocity, and depends entirely on someone actually reading the reports.
The defensible posture ties blocking to confidence. Block on surfaces with stable, well-thresholded coverage where a red diff reliably means something. Report-only while a new axis or a new surface is still shaking out and producing noise that would otherwise block merges for no reason. The key word is while — report-only is explicitly temporary, with a plan to promote it to blocking once the noise settles. A permanent report-only stage risks decaying into an ignored one: the reports can pile up unread, the failures become background, and the stage can end up costing runtime while catching little — at which point it is worse than not having it.
The honest limit sits above the pipeline entirely. The gate can require that someone signed off on a diff before merge; it cannot enforce that the images were opened, let alone understood. A blocking stage that a reviewer clears by clicking approve without opening the images is, from the pipeline's perspective, indistinguishable from a careful review. Review quality stays a human responsibility, and the pipeline's job is the narrower one of making sure the judgment is required, the evidence is present, and the update path does not let anyone skip the step.