DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk5 min

Visual Testing with Vitest: How to Catch UI Regressions

Use Vitest Browser Mode’s toMatchScreenshot() to catch UI appearance changes, with reviewed baselines and a standardized rendering environment.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Vitest 4’s Browser Mode can compare a rendered element or page with a reference screenshot using toMatchScreenshot(). Start with a focused UI state, create and inspect its baseline, then run comparisons in a consistent browser environment. Screenshot tests catch appearance changes; pair them with behavior assertions to check that the interface still works.

What Vitest visual regression tests check

A visual assertion captures the rendered UI and compares it with a stored reference image. It can reveal changes in layout, color, typography, spacing, or other visible details. It does not prove that a button submits a form, that keyboard navigation works, or that application logic is correct; keep interaction and semantic assertions as separate, complementary tests. See Vitest’s Visual Regression Testing guide.

As an Amazon Associate I earn from qualifying purchases.

Vitest 4 introduced visual regression support in Browser Mode. The current guide and API document toMatchScreenshot(); check the documentation for the Vitest version installed in your project because provider setup and API details can change. See Vitest 4.0 is out! and the Browser Mode Assertion API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set up Vitest Browser Mode

Browser Mode runs tests in a browser and requires a provider. Vitest documents preview, Playwright, and WebdriverIO options. For CI, install Playwright or WebdriverIO; Vitest recommends Playwright as a starting point if you do not already use one of them. Follow the Browser Mode installation guide for the package manager and configuration that match your project and installed Vitest version.

The exact provider configuration and install commands depend on that setup, so do not copy an unrelated configuration blindly. Once Browser Mode is configured, a focused test can look like this:

import { expect, test } from 'vitest'
import { page } from 'vitest/browser'

test('button looks correct', async () => {
  const button = page.getByRole('button')
  await expect(button).toMatchScreenshot('primary-button')
})

Render the intended state before making the assertion. Give the screenshot a descriptive name, and select a stable component or region unless the whole-page composition is what you need to protect. The Vitest Snapshot guide covers the assertion context.

Create and update screenshot baselines

First run

On its first run, Vitest creates a reference screenshot and fails the test because no reference existed. Inspect the image to confirm it shows the intended UI state, then commit it alongside the test. By default, Vitest places screenshots in __screenshots__ directories beside tests; browser and platform naming distinguishes captures.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Intentional design changes

When a planned UI change alters the image, use the documented update flow. For a Browser Mode project named vrt, the guide gives vitest --project vrt --update as an example. Review the resulting images before committing them; an update command replaces expected output, not a judgment that the new design is correct. Generate updates in the same controlled environment used for comparison where possible.

Stale files

Renaming or deleting tests can leave screenshot files behind. Check the corresponding __screenshots__ directory and remove obsolete references manually so the repository does not retain misleading baselines.

Make captures repeatable

Rendered pixels can vary with browser, operating system, fonts, GPU, resolution, and execution mode. Standardize the environment used to create and compare references, and pin browser and tool versions in CI where appropriate. A baseline generated on one machine may not be a reliable reference for a differently configured runner.

Vitest’s stability strategy takes repeated captures and compares consecutive images until the page settles or a timeout is reached. This helps with asynchronous image loading, animation, font rendering, and layout shifts, but a region that changes continuously can still time out.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Limit capture scope: Prefer the component or region whose appearance matters. Use full-page screenshots when the whole-page layout is the requirement.
  • Control dynamic data: Mock data sources or mask volatile elements when the chosen provider supports it.
  • Control animation: Vitest says animations are disabled by default for its built-in assertion with the Playwright provider and documents additional CSS-based control. Check the guide for the configuration applicable to your provider.
  • Wait for the intended state: Ensure content and fonts have loaded and layout has settled before interpreting a failure as a design regression.

Choose a comparison tolerance

Vitest documents the pixelmatch comparator, including a color threshold and limits on mismatched pixels by count or ratio. A ratio-based limit can scale with image size. If both a mismatch ratio and an absolute pixel limit are set, the stricter limit applies.

There is no universal tolerance prescribed by Vitest. Begin with a controlled rendering environment, observe its remaining noise, and set the strictest tolerance that does not flag that noise. Loosening a threshold can hide meaningful visual changes, so investigate differences before changing it. The guide also documents alternative comparators, including perceptual similarity metrics; use one only when pixel comparison remains noisy despite reasonable stabilization, and account for the fact that a different metric defines regressions differently.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Read a failed screenshot comparison

A failure can provide the stored reference, the actual capture, and a diff image. The diff is available when image dimensions match. Compare the images to decide whether the result is an unintended defect, an intentional design change that needs a reviewed baseline update, or rendering noise.

  • A large, coherent changed area often points to a real layout or styling change; inspect the component state and recent code changes.
  • Small differences around text edges may be rendering variation, but verify browser, platform, fonts, and execution mode before adjusting tolerance.
  • If dimensions differ, first check viewport, content growth, and capture scope; a pixel diff may not be available for mismatched sizes.

Troubleshoot flaky or failing visual tests

  • First run says the reference is missing: This is expected when Vitest creates the initial baseline. Inspect the generated image and commit it if it represents the intended state.
  • Images differ between local runs and CI: Align browser, operating system, fonts, resolution, and execution mode. Generate and compare baselines in the same standardized environment.
  • The test times out while waiting for stability: Look for content that keeps changing, including animations or live data. Stabilize or mock it, or narrow the capture to a stable region.
  • Diff image is unavailable: Check whether reference and actual image dimensions match. Investigate viewport and page-size changes before comparing pixel-level differences.
  • Many tiny differences appear near text: Check rendering environment consistency first. Only then assess whether a carefully chosen tolerance is justified by the remaining noise.
  • A test passes despite a broken interaction: A screenshot checks appearance, not behavior. Add assertions for role, state, keyboard use, or the interaction’s outcome.

Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server; it is an alternative for capturing pages, not a replacement for Vitest’s in-test visual assertions or committed baselines. One GET request can return a screenshot or PDF. For repeatable UI testing, you still need to manage your reference images and comparison workflow.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, this cURL call saves a WebP screenshot of a page. See the ScreenshotNeo documentation for API options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify the page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.