Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vitest 3.x reference skill covering configuration, test/describe APIs, mocking, coverage, snapshots, and concurrency.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/features-snapshots.md
1---2name: snapshot-testing3description: Snapshot testing with file, inline, and file snapshots4---56# Snapshot Testing78Snapshot tests capture output and compare against stored references.910## Basic Snapshot1112```ts13import { expect, test } from 'vitest'1415test('snapshot', () => {16const result = generateOutput()17expect(result).toMatchSnapshot()18})19```2021First run creates `.snap` file:2223```js24// __snapshots__/test.spec.ts.snap25exports['snapshot 1'] = `26{27"id": 1,28"name": "test"29}30`31```3233## Inline Snapshots3435Stored directly in test file:3637```ts38test('inline snapshot', () => {39const data = { foo: 'bar' }40expect(data).toMatchInlineSnapshot()41})42```4344Vitest updates the test file:4546```ts47test('inline snapshot', () => {48const data = { foo: 'bar' }49expect(data).toMatchInlineSnapshot(`50{51"foo": "bar",52}53`)54})55```5657## File Snapshots5859Compare against explicit file:6061```ts62test('render html', async () => {63const html = renderComponent()64await expect(html).toMatchFileSnapshot('./expected/component.html')65})66```6768## Snapshot Hints6970Add descriptive hints:7172```ts73test('multiple snapshots', () => {74expect(header).toMatchSnapshot('header')75expect(body).toMatchSnapshot('body content')76expect(footer).toMatchSnapshot('footer')77})78```7980## Object Shape Matching8182Match partial structure:8384```ts85test('shape snapshot', () => {86const data = {87id: Math.random(),88created: new Date(),89name: 'test'90}9192expect(data).toMatchSnapshot({93id: expect.any(Number),94created: expect.any(Date),95})96})97```9899## Error Snapshots100101```ts102test('error message', () => {103expect(() => {104throw new Error('Something went wrong')105}).toThrowErrorMatchingSnapshot()106})107108test('inline error', () => {109expect(() => {110throw new Error('Bad input')111}).toThrowErrorMatchingInlineSnapshot(`[Error: Bad input]`)112})113```114115## Updating Snapshots116117```bash118# Update all snapshots119vitest -u120vitest --update121122# In watch mode, press 'u' to update failed snapshots123```124125## Custom Serializers126127Add custom snapshot formatting:128129```ts130expect.addSnapshotSerializer({131test(val) {132return val && typeof val.toJSON === 'function'133},134serialize(val, config, indentation, depth, refs, printer) {135return printer(val.toJSON(), config, indentation, depth, refs)136},137})138```139140Or via config:141142```ts143// vitest.config.ts144defineConfig({145test: {146snapshotSerializers: ['./my-serializer.ts'],147},148})149```150151## Snapshot Format Options152153```ts154defineConfig({155test: {156snapshotFormat: {157printBasicPrototype: false, // Don't print Array/Object prototypes158escapeString: false,159},160},161})162```163164## Concurrent Test Snapshots165166Use context's expect:167168```ts169test.concurrent('concurrent 1', async ({ expect }) => {170expect(await getData()).toMatchSnapshot()171})172173test.concurrent('concurrent 2', async ({ expect }) => {174expect(await getOther()).toMatchSnapshot()175})176```177178## Snapshot File Location179180Default: `__snapshots__/<test-file>.snap`181182Customize:183184```ts185defineConfig({186test: {187resolveSnapshotPath: (testPath, snapExtension) => {188return testPath.replace('__tests__', '__snapshots__') + snapExtension189},190},191})192```193194## Key Points195196- Commit snapshot files to version control197- Review snapshot changes in code review198- Use hints for multiple snapshots in one test199- Use `toMatchFileSnapshot` for large outputs (HTML, JSON)200- Inline snapshots auto-update in test file201- Use context's `expect` for concurrent tests202203<!--204Source references:205- https://vitest.dev/guide/snapshot.html206- https://vitest.dev/api/expect.html#tomatchsnapshot207-->208