vitest-snap

Matchers

Overview of all snapshot matchers.

Overview

vitest-snap extends Vitest's expect with four snapshot matchers. Each matcher serializes the received value to a file and diffs it on subsequent runs using Vitest's built-in toMatchFileSnapshot.

MatcherInput typeOutput format
toTextSnapshotstring (or coercible)Plain text file
toJsonSnapshotAny object or primitiveFormatted JSON file
toYamlSnapshotAny object or primitiveYAML file
toMarkdownSnapshotObject / array of objectsMarkdown table file

Common behaviour

All three matchers:

  • Auto-name snapshot files from the current test name (slugified to lowercase).
  • Accept an optional options object to override the output directory, filename, and more.
  • Are async — always await the call.
await expect(value).toJsonSnapshot();

File path resolution

Snapshot files are written relative to the test file:

<dir>/<slugified-test-name>[_arg1_arg2...]?[.ext]
  • dir defaults to ./snapshots.
  • args appends extra segments to distinguish parametric snapshots.
  • fileExtension is appended as-is (leading dot is optional).

Last updated on

On this page