vitest-snap

Redactions

Reference for all built-in redaction classes.

Overview

Redactions replace node values (rather than removing them) after filters have been applied. They are useful for masking volatile or sensitive data — dates, tokens, IDs — with stable placeholder strings.

import { ReplacedRedaction, SortedRedaction } from "vitest-snap";

await expect(data).toJsonSnapshot({
  redactions: [
    new ReplacedRedaction(".apiKey", "[REDACTED]"),
    new SortedRedaction(".tags"),
  ],
});

ValueRedaction

Replaces any node where a predicate function returns true.

Applied by default for Date instances: every Date is replaced with [Date_1], [Date_2], … Pass a custom redactions array to override.

Constructor

new ValueRedaction(
  predicate: (path: string, value: unknown) => boolean,
  replaced:  string,
  counting?: boolean,
)
ParameterTypeDefaultDescription
predicate(path, value) => booleanReturns true for nodes to redact
replacedstringReplacement string, e.g. "[Secret]"
countingbooleantrueWhen true, appends _1, _2, … to make each occurrence unique

Example — redact all strings longer than 100 characters

import { ValueRedaction } from "vitest-snap";

new ValueRedaction(
  (_, v) => typeof v === "string" && v.length > 100,
  "[LONG_STRING]",
);

Example — redact specific primitive value

new ValueRedaction((_, v) => v === "supersecret", "[REDACTED]", false);

Default Date redaction

The built-in default is equivalent to:

new ValueRedaction((_, v) => v instanceof Date, "[Date]");
// produces [Date_1], [Date_2], … for each Date encountered

ReplacedRedaction

Replaces all nodes matched by a selector with a fixed placeholder string.

Constructor

new ReplacedRedaction(
  selector: Selector<T>,
  replaced: string,
  counting?: boolean,
)
ParameterTypeDefaultDescription
selectorSelector<T>Path expression targeting nodes to redact
replacedstringReplacement string
countingbooleantrueAppends _1, _2, … per occurrence when true

Example — mask a top-level field

import { ReplacedRedaction } from "vitest-snap";

new ReplacedRedaction(".password", "[REDACTED]");

Example — mask every token in an array

new ReplacedRedaction(".sessions[].token", "[TOKEN]");

Example — fixed placeholder without counter

new ReplacedRedaction(".id", "[ID]", false);
// all .id values become "[ID]" (no _1, _2 suffix)

SortedRedaction

Sorts the array at the matched path in-place (ascending, using default JS comparison). Useful for stabilizing snapshot output when the order of array elements is non-deterministic.

Constructor

new SortedRedaction(selector: Selector<T>)
ParameterTypeDescription
selectorSelector<T>Path expression targeting an array to sort

Example — sort a top-level array of strings

import { SortedRedaction } from "vitest-snap";

const redaction = new SortedRedaction(".tags");

redaction.apply({ tags: ["banana", "apple", "cherry"] });
// → { tags: ["apple", "banana", "cherry"] }

Example — sort nested arrays

new SortedRedaction(".users[].roles");

Usage in snapshot

await expect(result).toJsonSnapshot({
  redactions: [new SortedRedaction(".items")],
});

Last updated on

On this page