snap

package
v0.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package snap is a minimal file-snapshot testing helper: Match compares a string against a snapshot stored under __snapshots__/ next to the calling test file, and Clean (wired via TestMain) removes snapshots no test uses anymore. In-house replacement for the subset of go-snaps that lstk uses.

Workflow: a missing snapshot is created on the first local run (never in CI, where it fails instead). On a mismatch the test fails with a diff; UPDATE_SNAPS=true go test ./... rewrites snapshots and lets Clean delete obsolete ones.

Storage is one archive per test file (__snapshots__/<test_file>.snap) with one section per Match call:

[TestName_1]
value
---

[TestName_2]
...

N is the Match call number within the test. A value's newlines are stored verbatim; the "---" terminator always sits on its own line, so a value not ending in a newline gets one — a snapshot therefore cannot distinguish a value from the same value plus one final newline. The format has no escaping: a value containing a line that is exactly "---" cannot be stored; write guards against that with a parse round-trip and fails with guidance to sanitize the value instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clean

func Clean(m *testing.M) int

Clean runs the package's tests and then handles obsolete snapshots: archive entries no Match call used, and whole .snap archives in visited snapshot directories that no test touched. They are deleted when UPDATE_SNAPS=true, otherwise reported as an error (non-zero exit) so stale snapshots can't linger unnoticed. Wire it in TestMain:

func TestMain(m *testing.M) { os.Exit(snap.Clean(m)) }

Cleanup is skipped on failed or filtered runs (-run/-skip), where "not visited" doesn't mean obsolete.

func Match

func Match(t testing.TB, got string)

Match compares got against the stored snapshot for the calling test, creating the snapshot on first local run or rewriting it when UPDATE_SNAPS=true.

func MatchJSON

func MatchJSON(t testing.TB, got []byte, maskPaths ...string)

MatchJSON snapshots got (a JSON document) in canonical pretty-printed form (two-space indent, object keys sorted by encoding/json), after replacing the values at the given dotted paths (e.g. "data.currentVersion") with the placeholder "<any>". Use the paths to mask values that legitimately change between runs. A path that doesn't resolve fails the test, so a masked field disappearing is caught rather than silently ignored. Paths traverse JSON objects only; there is no array-index syntax.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL