Skip to content

Introduction

What is visual regression testing?

Visual regression testing compares screenshots of your UI with approved ones to catch unintended changes: how it works, what it catches, flaky screenshots.

Visual regression testing checks that your user interface still looks the way it should. Your tests take screenshots of pages or components, and each one is compared with an approved version of the same screenshot, called the baseline. When the two differ, a person looks at the difference and decides whether it was meant: if it was, the new screenshot is approved and becomes the baseline; if it wasn't, it's a bug, a visual regression.

Unit and end-to-end tests check behavior: a button exists, a click submits a form, a total is right. They pass just as happily when the button is hidden behind a banner, the text overflows its box or the page has lost its styles. Visual regression testing catches what a person would notice at a glance:

  • a CSS change that moves or restyles something on another page;
  • a layout that breaks at one screen width;
  • a font, icon or image that fails to load;
  • a dependency upgrade (a component library, a browser) that changes how things render;
  • a component that looks wrong in one of its states, such as empty, loading or in dark mode.

It doesn't replace review of the code. It shows reviewers the result of the code, so a change to one shared style shows up on every screenshot it touched.

  1. Capture. A test opens a page or renders a component in a browser and saves a screenshot under a stable name, such as checkout/payment-form. This usually runs in CI on every commit or pull request.
  2. Compare. Each screenshot is compared with the baseline of the same name. Byte-identical images are unchanged; otherwise the pixels are compared, and an image is produced that highlights what changed.
  3. Review. Changed, added and removed screenshots are shown to the team side by side with their baselines. Each change is approved or rejected.
  4. Gate. Until the changes are reviewed, the pull request can't merge. Approved screenshots become the baselines that later builds are compared with.

CirCI's version of each step: How a build flows, How screenshots are compared and Baselines and ignore regions.

The baseline should be what the code you're changing looked like. For a pull request, that's the approved screenshots of the commit the branch started from, not the newest ones on main, which may include changes your branch doesn't have yet. CirCI walks the commit history back from where the branch started and uses the nearest approved build (How a build gets its baseline).

Comparing raw pixel values flags differences no one can see, such as a color one shade off or an edge smoothed slightly differently. Tools usually compare with some tolerance instead:

  • A color tolerance. CirCI uses a perceptual color distance, so a pixel only counts as changed when the difference is visible, and it leaves out pixels that differ only by anti-aliasing.
  • A threshold. The share of changed pixels a screenshot may have and still count as unchanged. CirCI's default is 0%: one changed pixel is a change. Raise it per project if small, harmless differences are common.
  • Ignore regions. Rectangles that are never compared, for a clock, an ad or a live chart.

The detail of CirCI's comparison, and every setting that changes the result, is in How screenshots are compared.

A screenshot is flaky when it comes out different without the code changing. The usual causes:

  • Time and data. Dates, relative times ("3 minutes ago"), random IDs, data loaded from a live service.
  • Motion. Animations, transitions, carousels, blinking cursors, images still loading when the screenshot is taken.
  • The environment. A different browser version, operating system, font set or screen density renders text and edges differently.

Most fixes make the page render the same every time: freeze the clock, use fixed test data, disable animations, wait for fonts and images, and take every screenshot in the same browser and container. What can't be made stable can be left out with an ignore region. CirCI also notices screenshots that keep flipping back to an image they already had, marks them flaky, and can let a pull request through when flaky screenshots are the only change (Triage and flaky screenshots).

The screenshots can come from the tests you already have:

  • Components, one screenshot per story: Storybook, or Vitest in browser mode.
  • Pages and flows, from end-to-end tests: Playwright or Cypress.
  • Anything else that writes PNG files, uploaded with the circi CLI.

Component screenshots are fast and isolate each state; page screenshots catch how components fit together. Many teams use both.

The comparison and review need somewhere to run. Hosted services (Chromatic, Percy, Argos and others) run it for you, and your screenshots are stored on their servers. A self-hosted tool such as CirCI runs on your own servers, so the screenshots of internal apps stay with you, and you take on running it. Visual regression testing without a SaaS weighs the two, and Compared with Chromatic, Percy and Argos lists the differences.