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 flag | Value / default | Applies to | Source |
|---|---|---|---|
browser.enabled | true | Current Vitest docs | Browser Mode |
browser.provider | playwright() imported from @vitest/browser-playwright; automation alternative webdriverio() | Return value of the provider factory | 4.0 announcement, provider |
browser.instances | At least one; example [{ browser: 'chromium' }] | Current Vitest docs | Browser Mode |
browser.headless | Use true for controlled CI runs; keep baseline generation mode consistent | Current Vitest docs | Browser Mode |
comparatorName | Explicit "pixelmatch" for the built-in comparator and option type inference | Current matcher API | assertions |
comparatorOptions.allowedMismatchedPixelRatio | 0.01 permits 1% differing pixels; example policy, not a default; valid range 0–1 | Current matcher API | assertions |
browser.expect.toMatchScreenshot.screenshotDirectory | Default "__screenshots__"; reference directory | Current config | browser.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 symptom | Cause | Fix | Source |
|---|
| 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