vitest-snap

Configuration

Configure snapshot options globally or per matcher call.

Overview

Options are merged in this order (last wins):

  1. Built-in defaults
  2. configureGlobalSnapOptions values
  3. Per-call options argument

Default configuration

All matchers share the same built-in defaults:

OptionDefaultDescription
dir"./snapshots"Directory relative to test file
filters[new UndefinedFilter()]Removes undefined props
redactions[new ValueRedaction(isDate, "[Date]")]Replaces Date values with a tag
indent2Spaces used by JSON and YAML formatting

name, fileExtension, and args have no built-in default — they are derived from the test name / left empty unless explicitly set.

Global configuration

Use configureGlobalSnapOptions in your Vitest setup file to set process-wide defaults:

vitest.setup.ts
import { configureGlobalSnapOptions, NullFilter } from "vitest-snap";

configureGlobalSnapOptions({
  dir: "./__snapshots__",
  filters: [new NullFilter()],
});

Register the setup file in your Vitest config:

vitest.config.ts
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    setupFiles: ["./vitest.setup.ts"],
  },
});

Global options are shallow-merged with the built-in defaults, then any per-call options are merged on top.

Matcher configuration

Pass an options object as the first argument to any matcher to override options for that single call:

test("user object", async () => {
  await expect(user).toJsonSnapshot({
    dir: "./__snapshots__",
    name: "my-custom-name",
    fileExtension: "json",
    args: [userId],
    filters: [new NullFilter()],
    redactions: [new ReplacedRedaction(".password", "[REDACTED]")],
    indent: 4,
  });
});

Per-call options always win over global and built-in defaults.

All options

OptionTypeMatchersDescription
dirstringJSON, Text, YAML, MarkdownDirectory (relative to test file) where snapshot files are written
namestringJSON, Text, YAML, MarkdownBase filename slug — defaults to slugified test name
fileExtensionstringJSON, Text, YAML, MarkdownExtension appended to the filename (e.g. "json", "txt")
argsany[]JSON, Text, YAML, MarkdownExtra segments for parametric snapshots (e.g. [pageSize]_50)
filters(Selector<T> | Filter<T>)[]JSON, YAML, MarkdownFilters applied before serialization — see Filters
redactionsRedaction<T>[]JSON, YAML, MarkdownRedactions applied after filters — see Redactions
indentnumberJSON, YAMLSpaces used by JSON and YAML formatting

Last updated on

On this page