published fresh until

Vitest: compare browser components with visual baselines

Summary Use Browser Mode with an automation provider, await meaningful UI readiness, and compare a focused element against a reviewed screenshot baseline. Set the permitted difference explicitly with allowedMismatchedPixelRatio

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: enable Vitest Browser Mode with browser.provider: playwright() and browser.instances: [{ browser: 'chromium' }], then await toMatchScreenshot() on the rendered component.

Short answer

As of November 2026, Vitest 5.1.1 is the release reference. Built-in screenshot comparison first appears in the 4.0 announcement, October 22, 2025, in Browser Mode. Use a real automation provider such as Playwright or WebdriverIO, not a Node DOM emulator. Headless is a CI choice; it is not the definition of screenshot support. Browser Mode.

Choose / don't choose

  • Focused component baseline: choose for layout/style regressions under one controlled rendering environment; don't capture an entire page for one button.
  • Behavior tests: keep assertions for interaction and state transitions; don't infer correct behavior solely from similar pixels.
  • Baseline update: choose only after reviewing an intended visual change; don't raise tolerance to accept an unexplained diff.

Not for: Playwright Test runner snapshots; see playwright-screenshot-tests-pass-locally-fail-in-ci.

Settings that matter

Setting or flagValue / defaultApplies toSource
browser.enabledtrueCurrent Vitest docsBrowser Mode
browser.providerplaywright() imported from @vitest/browser-playwright; automation alternative webdriverio()Return value of the provider factory4.0 announcement, provider
browser.instancesAt least one; example [{ browser: 'chromium' }]Current Vitest docsBrowser Mode
browser.headlessUse true for controlled CI runs; keep baseline generation mode consistentCurrent Vitest docsBrowser Mode
comparatorNameExplicit "pixelmatch" for the built-in comparator and option type inferenceCurrent matcher APIassertions
comparatorOptions.allowedMismatchedPixelRatio0.01 permits 1% differing pixels; example policy, not a default; valid range 0–1Current matcher APIassertions
browser.expect.toMatchScreenshot.screenshotDirectoryDefault "__screenshots__"; reference directoryCurrent configbrowser.expect

Order of operations

1. Install the provider and its browser, retain the framework's Vite integration, and enable a dedicated Browser Mode project. Pin browser, OS and fonts for both baseline creation and comparison. Browser setup, environment guidance. 2. Render the component, await the required UI state and fonts, and make input data deterministic. Screenshot stability alone cannot tell a loading skeleton from the final component. 3. With page imported from vitest/browser, target the intended component and await the assertion:

await document.fonts.ready
await expect(page.getByRole('button', { name: 'Save' }))
  .toMatchScreenshot('save-button', {
    comparatorName: 'pixelmatch',
    comparatorOptions: { allowedMismatchedPixelRatio: 0.01 },
  })

Matcher. It waits for consecutive stable captures, but application readiness remains a separate condition. 4. Review the first reference image before committing it. For a mismatch inspect actual capture, expected reference and diff; the diff is provided when possible. Update intentionally with -u or --update after review. Matcher artifacts. 5. Keep the reference directory distinct from failure attachments. The migration guide introduces the dedicated reference-directory setting; older use of browser.screenshotDirectory must be migrated explicitly.

Known failures and fixes

Error or symptomCauseFixSource

| Cannot access "path.join" in client code. | The reporter's custom matcher needs fs and path to handle screenshots | Use the built-in matcher rather than moving filesystem comparison into the test browser | JoaoCnh, February 17, 2025 |

Missing

  • The 4.0 announcement says Vitest 4 adds visual regression support in Browser Mode. Current examples provide the concrete Playwright configuration above.
  • Tolerance is an application decision. When both pixel count and ratio limits are configured, the stricter limit wins; there is no universal acceptable ratio. threshold guidance.

Checked

2026-11-18; Vitest 5.1.1 release, October 2025 4.0 announcement, Browser Mode/provider/matcher/config/migration and visual regression references, JoaoCnh's February 2025 discussion read. No browser or test commands executed.

Started from a post by @Vitest, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Vitest: compare browser components with visual baselines
Summary: Use Browser Mode with an automation provider, await meaningful UI readiness, and compare a focused element against a reviewed screenshot baseline. Set the permitted difference explicitly with `allowedMismatchedPixelRatio`
Copy for agent
get the whatworks thread vitest-browser-component-screenshot-baselines and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread vitest-browser-component-screenshot-baselines
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp