/web-testing-visual-regression:web-testing-visual-regressionfrom web-testing-visual-regression
Guides visual regression testing setup: screenshot baselines, determinism, diff review, baseline custody, and CI wiring. Useful when adding or debugging tests that compare rendered output against approved images.
Ships in web-testing-visual-regression. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Quick Guide: A visual test asserts that a subject still looks the way a human approved it. The comparison is trivial; everything hard surrounds it — making the render deterministic, scoping the capture to the subject, generating baselines in the environment that will diff them, and looking at every diff before accepting a new one. A baseline accepted without being looked at turns the suite into a machine that asserts the bug.
Detailed Resources:
The two families differ in where the baseline lives, who accepts a change, and who owns the render environment. Pick before writing anything; retrofitting is a migration rather than a flag.
Running both over the same subjects is the one combination to avoid — two baselines, two review flows, two chances to accept the wrong one. Splitting them by subject (a state catalog in the service, a handful of critical full-page checks committed) is common and works.
<critical_requirements>
Look at the diff before accepting any baseline. The approved image is the entire assertion, so accepting one unseen permanently encodes whatever was on screen, regression included, and the suite can never report it again.
Make the render deterministic before capturing — fonts loaded, motion stopped, clock frozen, randomness seeded, data fixed. A pixel that varies between runs is being asserted as though it were part of the design.
Generate and compare baselines in the same environment. Font rasterization and subpixel rendering differ between operating systems and container images, so a baseline made anywhere else shows a full-frame difference on every subject from the first run.
Capture the subject, not the page around it. An element or a clipped band fails only when it changes, which is what makes the failure name its own cause.
Mask a region that legitimately varies rather than widening the tolerance. A tolerance wide enough to absorb a live timestamp is wide enough to absorb a collapsed column.
</critical_requirements>
Auto-detection: visual regression, visual testing, screenshot testing, screenshot baseline, baseline image, golden image, pixel diff, image diff, expected/actual/diff, maxDiffPixels, maxDiffPixelRatio, diffThreshold, update snapshots, accept baseline, snapshot review, UI diff, stabilizing stylesheet
Applies to:
Handled elsewhere:
Every other kind of test states its intent in code: expect(total).toBe(42) says what correct means. A visual test states its intent in a file — an approved image somebody looked at once. Three consequences follow.
The baseline is a review artifact, not a build artifact. It records the judgement of a human who decided the UI was right. Regenerating it without looking destroys the only thing the test knows.
Nondeterminism is a false requirement, not flake. A pixel that varies between runs is being asserted as part of the design. Pin it or mask it; never widen the tolerance until it stops complaining.
The diff is the output. A suite that fails without producing a viewable expected/actual/diff triplet is unactionable, and unactionable suites get disabled.
What visual tests are good at is whole-appearance properties no reasonable assertion expresses — a shadow that vanished, a 3px shift that broke alignment, a font that failed to load, a dark-theme token that resolved to white-on-white, a container that stopped clipping overflow.
What they are bad at is anything you could name. If toBeVisible(), a text assertion or an accessibility-tree assertion would express it, write that instead.
The snippets below are portable shapes, not runnable code.
matchScreenshot(subject, name, options)stands for your harness's screenshot assertion, andfreezeClockAt,goto,byTestId,byRoleand the barepagestand for its clock, navigation and locator controls. Platform APIs such asdocument.fonts.readyare named as themselves. The option names are the ones the common pixel comparators share; confirm every spelling against your harness's own documentation. examples/determinism.md works the same patterns through fuller call shapes, which are closer to what you will actually type.
Tolerances, path layout and the baseline matrix belong in configuration. Per-assertion options are for what genuinely varies by subject — the mask list, the clip box.
// visual.config.ts
const PIXEL_THRESHOLD = 0.2; // per-pixel colour distance, 0-1
const MAX_DIFF_PIXEL_RATIO = 0.01; // share of the image allowed to differ
export default {
screenshots: {
threshold: PIXEL_THRESHOLD,
maxDiffPixelRatio: MAX_DIFF_PIXEL_RATIO,
stylePath: "./visual/stabilize.css", // injected into every capture
},
};
The two knobs answer different questions and are not interchangeable. threshold decides whether a single pixel counts as different, which is what anti-aliasing needs. maxDiffPixels / maxDiffPixelRatio decide how many differing pixels are tolerated, and nothing needs those above about 1% except a subject that should have been masked.
Full code: examples/core.md
One test, N approved images. The configuration cell's name enters the baseline path, so each cell is approved independently.
projects: [
{ name: "desktop-light", use: { viewport: DESKTOP, colorScheme: "light" } },
{ name: "desktop-dark", use: { viewport: DESKTOP, colorScheme: "dark" } },
{ name: "mobile-light", use: { viewport: MOBILE, colorScheme: "light" } },
{
name: "second-browser",
use: { browser: "firefox" },
ignoreSnapshots: true,
},
];
Duplicating a test per theme means every subject is written twice and the one nobody remembers to add silently stops covering anything. ignoreSnapshots keeps a browser in the functional run without a second baseline set to maintain.
Full code: examples/core.md
Six sources of nondeterminism account for nearly every flaky visual test, and each has a fix cheaper than the flake.
| Source | Symptom in the diff | Fix |
|---|---|---|
| Web fonts | Whole text blocks shift; fallback metrics | Self-host and preload, then await document.fonts.ready |
| Animation/transition | Random intermediate frames | Disable CSS motion at capture; stop JS-driven motion too |
| Time | "2 minutes ago", clocks, date-stamped rows | Freeze the clock before navigating — freeze, do not fast-forward |
| Randomness | Shuffled lists, random ids, placeholder avatars | Seed the generator before any application script runs |
| Data | Row order and counts vary per run | Serve fixed fixtures with stable ordering and identifiers |
| Scrollbars / DPR | ~15px width shift, blurry text at 2× | Hide scrollbars in the stabilizing stylesheet; pin scale |
await freezeClockAt(new Date("2026-01-15T12:00:00Z")); // before navigation
await goto(DASHBOARD_URL);
await page.evaluate(() => document.fonts.ready); // a real signal, not a guessed delay
await matchScreenshot(summaryRegion, "summary.png");
The seventh source is the machine. Baselines generated on a laptop and diffed in CI produce a permanent full-frame difference on every subject, which trains the team to regenerate blindly, which destroys the suite.
Full code: examples/determinism.md
| Subject | Capture |
|---|---|
| A component or one of its states | Element capture |
| A band or region spanning components | Page capture with clip |
| Whole-page composition (landing, invoice) | Page capture with fullPage |
| Anything above the fold | Page capture, default viewport |
await matchScreenshot(
page.getByRole("region", { name: /pro plan/i }),
"pricing-card-pro.png",
);
await matchScreenshot(page, "header-band.png", {
clip: { x: 0, y: 0, width: 1280, height: 96 },
});
A full-page capture used to prove a button changed fails on every unrelated header edit — and fails every other full-page baseline at the same time, so the reviewer sees dozens of red subjects, no signal, and the fastest way out is a blanket regenerate.
Full code: examples/core.md
await matchScreenshot(page, "dashboard.png", {
mask: [byTestId("last-synced-at"), byTestId("live-visitor-count")],
maskColor: "#000000", // defaults are often magenta, which some UIs legitimately contain
});
The mask list doubles as documentation of exactly which parts of the frame are not covered. Raising maxDiffPixelRatio until the timestamp stops failing buys enough slack to hide a missing sidebar, and says nothing about where the slack went.
Where a volatile region has no stable handle to mask, hide it in the stabilizing stylesheet instead — that needs only a selector.
Full code: examples/core.md
Three legitimate events — create (a new subject appears), accept (an intended change is reviewed and approved), retire (the subject is deleted and its images go with it). Anything else is drift.
# WRONG - the "visual tests are failing" reflex
npm run test:visual -- --update-all
# RIGHT - look first
npm run test:visual # emits expected/actual/diff per failure
npm run test:visual:report # inspect every diff
npm run test:visual -- --update-changed # only after each one is understood
Updating only mismatched baselines is not the same as updating everything: the blanket mode rewrites matching baselines too, silently resetting subjects nobody inspected.
The update travels with the change. New images belong in the same pull request as the code that changed the appearance. A separate "update baselines" commit is unreviewable — nobody can tell an intended redesign from a regression once the two are in different diffs.
Ownership is explicit. The author who changed the UI proposes baselines and says in the description what should have changed; someone else accepts. Author self-acceptance removes the only human judgement in the loop.
Full code: examples/ci.md
Visual checks run on pull requests and on every trunk build — trunk builds are what keep baselines valid through merges. They skip documentation-only and config-only changes, and they never run in update mode in an automated job.
# Pipeline definition; the syntax varies by provider, the shape does not
- name: Visual tests
run: npm run test:visual
- name: Publish diffs
if: always-except-success-only # must also fire on timeout and cancellation
# upload the report and results directories as a build artifact
A job whose only output is "1 failed" forces a local reproduction of a container-specific render, so the reviewer regenerates instead — which is the failure this whole skill exists to prevent.
Treat the matrix as a budget. Every new cell multiplies the whole corpus: its capture time, its review surface, and its bill. Add one when a real class of bug lives there, and delete one that has never caught anything.
Full code: examples/ci.md
<red_flags>
Breaks at runtime:
step-3.png) — reordering silently reassigns approved images to different subjectsSurprising behaviour:
clip and fullPage exist on the page-level capture only; for an element, capture the element rather than computing its box</red_flags>
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub agents-inc/skills --plugin web-testing-visual-regressionOther plugins' skills for the same job