Snapshots
Snapshots pin the serialized form of a value so you notice when it changes. They suit output that is tedious to assert field by field — rendered strings, serialized structures, large arrays.
Basic usage
import { expect, test } from "as-test";
test("renders a report", () => {
expect(renderReport(data)).toMatchSnapshot();
});The first run has nothing to compare against, so record the baseline:
ast test --create-snapshotsSubsequent runs compare the serialized value against the stored snapshot and fail on any difference.
Naming and identity
A snapshot is keyed by its file, its suite nesting path, and a name:
- Unnamed snapshots are numbered per suite in source order —
#1,#2, … - Named snapshots use the string you pass:
expect(x).toMatchSnapshot("empty-state").
Because the key includes the suite path, the same name used in two different suites refers to two different snapshots. Prefer names once a suite has more than one snapshot — reordering unnamed snapshots renumbers them.
Updating snapshots
Snapshots live under the configured snapshotDir (default .as-test/snapshots). Manage them from the CLI rather than editing files:
| Flag | Effect |
|---|---|
--create-snapshots | Record snapshots that don't exist yet. Existing ones still must match. |
--overwrite-snapshots | Accept the current output as the new baseline on mismatch. |
--no-snapshot | Skip snapshot assertions entirely for this run. |
ast test --create-snapshots # add new snapshots
ast test --overwrite-snapshots # intentional change: re-baselineThe run summary reports snapshot outcomes separately:
Snapshots: 3 matched, 1 created, 0 updated, 0 failedWhen to use them
- Good fits: serialized output, formatted strings, large or nested structures, regression guards on rendering.
- Poor fits: values that change every run (timestamps, random data, pointers). Pin those with a function mock or
snapshotFnfirst, or assert the parts that are stable.
Related
- Snapshots & Throws — the matcher.
- Stable Values — freeze a value so a snapshot stays deterministic.
