Vitest screenshot testing with toMatchScreenshot
Set up Vitest screenshot testing in Browser Mode, create and review baselines with toMatchScreenshot, and troubleshoot missing references and CI image differences.
Vitest screenshot testing uses toMatchScreenshot in Browser Mode to compare rendered images with reviewed references. Use it for appearance checks, and keep ordinary assertions for behavior. A DOM snapshot from toMatchSnapshot stores a serialized value; it does not show whether CSS clips a label or moves a button.
Install and configure Vitest Browser Mode
This minimal example uses the Playwright provider and a plain DOM fixture, so no framework renderer is required. Install compatible Vitest and provider versions together and commit the resulting lockfile. In an existing React or Vue project, retain your Vite framework plugin and use your component renderer in place of the fixture.
npm install -D vitest @vitest/browser-playwright
npx playwright install chromiumCreate vitest.config.ts, or add a dedicated visual project to your existing configuration. The include pattern keeps this example separate from Node-only unit tests. Browser Mode supplies the rendering engine that jsdom does not provide.
import { defineConfig } from "vitest/config";
import { playwright } from "@vitest/browser-playwright";
export default defineConfig({
test: {
include: ["tests/**/*.visual.test.ts"],
browser: {
enabled: true,
headless: true,
provider: playwright(),
instances: [
{ browser: "chromium", viewport: { width: 1280, height: 720 } },
],
},
},
});Write a complete screenshot test
Save this as tests/button.visual.test.ts. It creates an actual visible button, captures it by its accessible name, and removes the fixture even if the assertion fails. For application coverage, render your own component and include its real styles.
import { expect, test } from "vitest";
import { page } from "vitest/browser";
test("save button appearance", async () => {
const button = document.createElement("button");
button.textContent = "Save changes";
button.style.cssText =
"padding:12px 20px;font:16px Arial;background:#153d64;color:white;border:0";
document.body.append(button);
try {
await document.fonts.ready;
await expect(page.getByRole("button", { name: "Save changes" }))
.toMatchScreenshot("save-button");
} finally {
button.remove();
}
});First run writes the reference image and fails the test — deliberately, so a baseline is never silently created by a passing run. Later runs compare against it.
# First run: inspect the new reference in tests/__screenshots__/.
npx vitest run
# Rerun after review, then commit the reference images.
npx vitest run
# Only for an intentional, reviewed visual change:
npx vitest run --updateKeep --update out of the ordinary CI check. When a reference changes, inspect the image diff in the pull request as carefully as the code. Passing after an update only proves that the new image became the reference; it does not establish that the new design is correct.
The platform-naming consequence
Like Playwright, Vitest includes the browser and platform in the screenshot name — button-chromium-darwin.png. So a baseline recorded on a developer's Mac is a different file from the one CI compares against on Linux, and updating the first does not update the second.
The fix is the same as everywhere else: record baselines only inside the container CI uses, and make that the sanctioned route.
# In the same pinned Linux environment used for reference creation:
npm ci
npx playwright install --with-deps chromium
npx vitest run --browser.headlessThe full set of causes and the order worth fixing them in is in how to fix flaky visual tests — all of it applies here unchanged.
Why the ecosystem is moving here
Three things converged, and if you are following a guide written before 2026 it probably predates all of them.
- Storybook has a Vitest addon. Vite-powered Storybook projects can run story tests through Browser Mode. Check the addon's framework and version requirements before migrating an existing test-runner setup.
- Browser mode runs real browsers. Component tests execute in an actual browser rather than jsdom, which is the prerequisite for meaningful screenshots — jsdom has no layout engine and cannot render.
- Comparison is built in.
toMatchScreenshotmeans no plugin assembly, which is the position Cypress users are still in — see Cypress visual regression testing.
With Storybook
If your components live in Storybook, the Vitest addon runs your stories as browser tests:
npx storybook add @storybook/addon-vitestRunning a story as an interaction test does not automatically add a screenshot assertion. Configure visual checks explicitly or use a compatible visual integration. Pilot one story with an intentional styling change and confirm that the captured result fails before assuming every story has visual coverage.
Vitest or Playwright?
| Vitest browser mode | Playwright | |
|---|---|---|
| Natural scope | Components, in isolation | Pages and flows, in a real app |
| Assertion | toMatchScreenshot | toHaveScreenshot |
| Screenshot stability | Waits for consecutive stable captures | Waits for consecutive stable captures |
| Engines | Via provider (Playwright or WebdriverIO) | Chromium, Firefox, WebKit natively |
| Storybook integration | First-class via the addon | Possible, less direct |
| Best when | You already run Vitest for unit tests | You already run Playwright end-to-end |
Troubleshooting Vitest screenshot tests
| Symptom | Likely issue | Check first |
|---|---|---|
| Matcher unavailable or no browser | The test is running in a Node project | Browser Mode enabled, provider installed, file included in the visual project |
| No existing reference screenshot | A new test or browser/platform combination | Inspect the generated image before committing it |
| Passes locally, fails in CI | Different renderer, fonts or reference path | Use the same pinned environment for recording and comparing |
| Screenshot never stabilizes | Live data, animation or delayed content | Seed data and wait for the exact visible state |
| Text snapshots pass but layout breaks | The test compares serialized DOM rather than pixels | Add toMatchScreenshot to the rendered element |
Do not swap in toMatchSnapshot merely to make a failing image check pass. The two assertions inspect different evidence. See snapshot testing versus visual regression testing for when each is useful.
In practice this is rarely a choice — you use whichever you already run. Many teams end up with both: Vitest for component-level checks, Playwright for the composed pages. That is a sensible split rather than a duplication, because they cover genuinely different risks.
What it still does not solve
Identical to every other framework-level tool, and worth stating plainly:
- You author every check. A component with no test is invisible.
- No built-in hosted approval workflow. Decide how reviewers will see image differences and record approval before references are updated.
- Baselines live in your repository as binaries, with the platform-naming trap above.
- Component coverage is not page coverage. A component passing in isolation says nothing about the page composing it.
Those are the trade-offs the hosted tools exist to absorb — compared in visual regression testing tools.
Frequently asked questions
Does Vitest support visual regression testing?
Yes, natively in browser mode through the toMatchScreenshot assertion. It captures a component or page screenshot and compares it against a stored reference. The first run creates the reference and deliberately fails, so baselines are never created silently by a passing test.
Why do Vitest screenshots differ between machines?
Vitest's own documentation lists the causes: GPU and drivers, operating system, font rendering pipelines, browser version and settings, headless versus headed mode, and screen scaling or colour profiles. It names screenshots per browser and platform for this reason and strongly recommends running them in Docker or a cloud service.
Should I use Vitest or Playwright for visual testing?
Vitest Browser Mode fits component checks in a Vite-based test setup. Playwright Test fits navigation and flows in a running application. Both provide screenshot stability checks; choose based on the surface you need to cover and the runner your team maintains.
Sources
- Vitest — Visual Regression Testing documentationchecked 2026-11-07
- Vitest Browser Mode installation and configurationchecked 2026-11-07
- Storybook Vitest addon documentationchecked 2026-11-07