Skip to content

What is visual regression testing?

Mikhail Bodnarchuk

Definition

Visual regression testing compares screenshots of a page or component with approved baseline images to catch unintended changes in how the interface looks. A test fails when too many pixels differ, and a person reviews the diff. It catches layout shifts, overlapping text and wrong colors that functional assertions pass.

How does visual regression testing work?

A functional test asks whether the button works. A visual test asks whether it still looks the way it did yesterday. The loop has five steps:

  1. Render a fixed state. The test opens a page or mounts a component with known data, viewport and fonts.
  2. Capture a screenshot. Usually of one element, sometimes of the whole page.
  3. Compare it with the baseline. A pixel-diff library compares the new image with the approved reference image stored in the repository.
  4. Review the diff. On a mismatch the runner saves the expected, actual and diff images, and a person decides: bug or intended change.
  5. Approve or fix. An intended change becomes the new baseline, which goes through code review like any other file.

In Playwright Test the assertion is toHaveScreenshot(). The first run has nothing to compare against, so it writes the reference image and fails; later runs compare against it (Playwright visual comparisons). Playwright waits for a stable picture: it takes screenshots "until two consecutive screenshots matched" and saves the last one. Intended changes are accepted with npx playwright test --update-snapshots.

How is a visual test different from a functional test?

Functional assertionVisual regression check
What it checksBehavior: the element exists, has the right text, reacts to a clickAppearance: pixels match an approved image
CatchesBroken logic, missing elements, wrong dataLayout shifts, overlapping text, wrong colors, broken CSS
MissesAnything that works but looks wrongWhy something changed, and whether users can interact with it
A failure meansThe product is brokenSomething looks different; a person decides if that is a bug
UpkeepChanges when behavior changesChanges with every intended visual change

The Vitest docs make the same split in plain words: toMatchScreenshot "is not a substitute for proper assertions", and a test that only takes a screenshot "is just documenting the current state" (Vitest visual regression testing). They recommend keeping the visual suite in its own project, so each suite "can fail loudly for the right reasons". Visual checks sit on top of a normal regression testing suite; they do not replace it.

Example: a 6-pixel shift fails, a new button color passes

We wrote one Playwright test for a checkout card. It checks behavior first (the Pay now button is visible), then compares a screenshot of the card with the committed baseline. The VARIANT variable picks the CSS that a "refactor" ships:

import { test, expect } from '@playwright/test'

// VARIANT picks the CSS a "refactor" ships: base, shade (button color nudged), shift (button pushed down 6px).
const css = {
  base: '',
  shade: '.pay { background: #55ba63 }',
  shift: '.pay { margin-top: 18px }',
}[process.env.VARIANT ?? 'base']

test('checkout card', async ({ page }) => {
  await page.setViewportSize({ width: 400, height: 240 })
  await page.setContent(`
    <style>
      body { font: 16px sans-serif; margin: 24px }
      .card { border: 1px solid #ccc; border-radius: 8px; padding: 16px; width: 300px }
      .pay { display: block; margin-top: 12px; padding: 10px 16px; border: 0; background: #5ecc62; color: #fff }
      ${css}
    </style>
    <div class="card"><strong>Total: $18.00</strong><button class="pay">Pay now</button></div>`)

  // The functional check passes in every variant.
  await expect(page.getByRole('button', { name: 'Pay now' })).toBeVisible()
  // The visual check compares against the committed baseline.
  await expect(page.locator('.card')).toHaveScreenshot('checkout-card.png', process.env.THRESHOLD ? { threshold: Number(process.env.THRESHOLD) } : {})
})

The first run wrote the baseline tests/checkout.spec.js-snapshots/checkout-card-darwin.png; the platform is part of the file name. Then we ran each variant. Run on 2026-11-19 with @playwright/test 1.65.1 (Chromium 159) on Node.js 22.23.5, macOS; excerpt, with the code frame and call log cut:

$ VARIANT=base npx playwright test
  ✓  1 tests/checkout.spec.js:10:1 › checkout card (430ms)
  1 passed (1.2s)

$ VARIANT=shade npx playwright test
  ✓  1 tests/checkout.spec.js:10:1 › checkout card (193ms)
  1 passed (692ms)

$ VARIANT=shift npx playwright test
  ✘  1 tests/checkout.spec.js:10:1 › checkout card (469ms)
    Error: expect(locator).toHaveScreenshot(expected) failed
      Expected an image 334px by 99px, received 334px by 105px. 1218 pixels (ratio 0.04 of all image pixels) are different.
  1 failed

$ VARIANT=shade THRESHOLD=0.05 npx playwright test
  ✘  1 tests/checkout.spec.js:10:1 › checkout card (441ms)
      2602 pixels (ratio 0.08 of all image pixels) are different.

Three results matter:

  • The functional assertion passed in all four runs. Only the screenshot noticed the moved button.
  • The new button color passed at the default settings. The shade change from #5ecc62 to #55ba63 stayed inside Playwright's default color tolerance, so the test was green.
  • A stricter threshold caught it. At threshold: 0.05, 2,602 pixels counted as different and the test failed.

How strict should the comparison be?

Both runners compare with the pixelmatch library and expose two kinds of tolerance:

  • Per-pixel color tolerance. In Playwright, threshold is "an acceptable perceived color difference between the same pixel in compared images, between 0 (strict) and 1 (lax)", with a default of 0.2. Our example shows what that default lets through.
  • How many pixels may differ. Playwright's maxDiffPixels is unset by default. Vitest "does not define a default tolerance for mismatched pixels" and recommends allowedMismatchedPixelRatio, so the limit scales with the screenshot size.

Pick values per component from the kind of change you must catch. A brand color drift needs a low color threshold; a text-heavy table may need a looser pixel ratio. When pixel diffs are too noisy, Vitest supports plugging in perceptual comparators such as SSIM.

Why do visual tests fail when nothing changed?

The Playwright docs warn that rendering "can vary based on the host OS, version, settings, hardware, power source (battery vs. power adapter), headless mode, and other factors." The fixes, from both docs:

  1. One environment for baselines and runs. Vitest strongly recommends Docker containers, CI-only visual runs or cloud services. A baseline made on a laptop can differ from a capture on a Linux CI runner.
  2. Hide what moves. Mask timestamps and user data, or apply a screenshot-only stylesheet with Playwright's stylePath. Move the mouse away so hover effects do not leak into the image.
  3. Disable animations. Playwright's call log in our run shows "disabled all CSS animations" before each capture.
  4. Capture elements, not pages. A full-page screenshot can fail on an unrelated change elsewhere on the page.
  5. Prune old baselines. Vitest does not delete screenshots of renamed or removed tests; stale ones pile up.

Intermittent visual failures are still flaky tests and need the same triage: a red run that people learn to rerun stops protecting anything.

When is visual regression testing worth it?

It pays off where appearance is the product and changes are rare: design systems, component libraries, checkout and sign-up flows, and big CSS refactors. It costs more than it returns on screens that change every sprint, or on dashboards whose data cannot be pinned. Every intended change there means reviewing and approving new images. Our review of AI testing tools lists open-source visual testing tools next to the built-in runner features shown here.

Our QA and test automation engineers set up the test framework (Codeception, CodeceptJS or Playwright), connect the suite to your CI with readable reports, and fix the waits, locators and test data behind unstable end-to-end runs.

Sources

  1. Visual comparisons, Playwright docs (Microsoft), 2026-09-29
  2. Visual Regression Testing, Vitest docs, 2026-09-28

Frequently asked questions

Is visual regression testing the same as snapshot testing?

Visual regression testing is a kind of snapshot testing where the snapshot is an image. Text snapshots, such as Playwright's toMatchSnapshot on a string, compare serialized output and say nothing about how the page renders.

Can visual regression testing replace manual UI review?

No. It only reports that pixels changed compared with an image someone already approved. A person still judges new designs, usability and whether a reported change is a bug.

Should visual tests run on every pull request?

Run them on pull requests that touch styles, components or layout, in the same container image that produced the baselines. A full visual suite on every commit can create more review work than it saves.

Where should baseline screenshots be stored?

In the repository next to the tests, so a change to a baseline is reviewed in the same pull request as the code that caused it. The Vitest docs suggest Git LFS for reference screenshots once the suite gets large.

Building with AI agents? Talk to an engineer.