Configuration
Configure snapshot options globally or per matcher call.
Overview
Options are merged in this order (last wins):
- Built-in defaults
configureGlobalSnapOptionsvalues- Per-call
optionsargument
Default configuration
All matchers share the same built-in defaults:
| Option | Default | Description |
|---|---|---|
dir | "./snapshots" | Directory relative to test file |
filters | [new UndefinedFilter()] | Removes undefined props |
redactions | [new ValueRedaction(isDate, "[Date]")] | Replaces Date values with a tag |
indent | 2 | Spaces 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:
import { configureGlobalSnapOptions, NullFilter } from "vitest-snap";
configureGlobalSnapOptions({
dir: "./__snapshots__",
filters: [new NullFilter()],
});Register the setup file in your Vitest config:
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
| Option | Type | Matchers | Description |
|---|---|---|---|
dir | string | JSON, Text, YAML, Markdown | Directory (relative to test file) where snapshot files are written |
name | string | JSON, Text, YAML, Markdown | Base filename slug — defaults to slugified test name |
fileExtension | string | JSON, Text, YAML, Markdown | Extension appended to the filename (e.g. "json", "txt") |
args | any[] | JSON, Text, YAML, Markdown | Extra segments for parametric snapshots (e.g. [pageSize] → _50) |
filters | (Selector<T> | Filter<T>)[] | JSON, YAML, Markdown | Filters applied before serialization — see Filters |
redactions | Redaction<T>[] | JSON, YAML, Markdown | Redactions applied after filters — see Redactions |
indent | number | JSON, YAML | Spaces used by JSON and YAML formatting |
Last updated on