Implementing and Updating Vitest Snapshot Tests
Learn how to create, verify, and safely update Vitest snapshot tests for component regression checks.
08 Feb 2026, 05:01 UTC

Desired Outcome
Run a Vitest test that asserts a component's rendered output matches a stored snapshot, and be able to update the snapshot safely when intentional changes occur.
Prerequisites
- Node.js ≥18 installed.
- Project with Vite and Vitest configured (e.g.,
npm init vite@latest my-app -- --template react-tsor similar). - A UI component or function whose output you want to snapshot (e.g., a React component
MyButton).
Procedure
- Install Vitest if not present (run in project root):
npm install --save-dev vitest - Add a test file next to the component, e.g.,
src/components/MyButton.test.tsx:import { render, screen } from '@testing-library/react'; import MyButton from './MyButton'; import { expect, test } from 'vitest'; test('renders MyButton with default props', () => { render(); const button = screen.getByRole('button'); expect(button).toMatchSnapshot(); }); - Run Vitest in watch mode to generate the initial snapshot:
The command creates anpx vitest__snapshots__folder beside the test file and writesMyButton.test.tsx.snapcontaining the serialized button markup. - Verify the snapshot: open the generated
.snapfile and confirm the markup matches what you expect (e.g., includes the button text and any default classes). - Introduce an intentional change (e.g., add a new CSS class to the button in
MyButton.tsx). - Run the test again:
You will see a failure indicating a snapshot mismatch, with a diff shown in the terminal.npx vitest - Review the diff: ensure the change is only the added class and nothing else.
- Update the snapshot (choose one):
- Interactive: press
uwhen Vitest prompts in watch mode. - Command line: run
npx vitest --update(ornpx vitest -u) to overwrite all stale snapshots.
- Interactive: press
- Confirm the test passes after the update.
- Optional: Add a custom serializer to trim whitespace or format complex objects. In
vitest.config.ts:
Then restart Vitest; new snapshots will use the formatter.import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { snapshotFormat: { // Example serializer that removes trailing spaces printBasicValue: value => String(value).replace(/\s+$/g, ''), }, }, });
Expected Checks
- Test passes after initial snapshot creation.
- After a deliberate component change, the test fails and shows a clear diff.
- Running
vitest --updateresolves the failure and the test passes again. - The
__snapshots__folder is tracked by your VCS (e.g., Git) and snapshots evolve with intentional changes.
Maintenance and CI Considerations
Snapshot files grow as tests evolve. Periodically run:
npx vitest --clean-snapshotsThis removes snapshot files that no longer have a corresponding test. Verify the output list before committing.
In CI, execute Vitest in non‑interactive mode to treat snapshot mismatches as failures:
npx vitest --runEnsure the
__snapshots__directory is checked into your repository so that all agents compare against the same baseline.If you use parallel test execution (default in Vitest), each worker gets its own temporary snapshot cache; the final comparison is still serialized, so no extra configuration is required. Check the Vitest logs for lines like “snapshot: worker 1” to confirm isolation.
Recovery Options
If you accidentally update a snapshot that should not have changed:
- Revert the snapshot file to its previous version using your VCS (e.g.,
git checkout HEAD -- src/components/__snapshots__/MyButton.test.tsx.snap).- Or run
npx vitest --restore-snapshotsif you have a backup copy.- Re‑run the test to confirm it now fails as expected, then re‑apply the intended component change and update the snapshot again.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.