package
0.0.0-20250107173743-d0474a929c0e
Repository: https://github.com/samsarahq/go.git
Documentation: pkg.go.dev

# README

Snapshotter

Snapshotter makes it easy to write tests that verify complex test output. To write a snapshot test, make a testhelpers.Snapshotter at the beginning of the test, set up a call to Verify() and call Snapshot(name, value) whenever you want to test a complex output. For example:

func TestWithComplexOutput(t *testing.T) {
    snapshotter := snapshotter.New(t)
    defer snapshotter.Verify()

    output := DoSomethingComplicated()
    snapshotter.Snapshot("complicated", output)
}

After writing the test, you can run it using go test .. However, the test will fail, because the snapshot isn't there yet, with an error like

--- FAIL: TestSnapshotter (0.00s)
    snapshotter.go:89: error reading snapshots: open testdata/TestSnapshotter.snapshots.json: no such file or directory

To generate the snapshots, run go test . -rewriteSnapshots or run REWRITE_SNAPSHOTS=1 go test .. This generates a set of snapshots files in the testdata directory that should be added to git. Then, the tests will pass. If it at some point a regression causes the test output to break, the snapshotter will catch the changes and fail:

--- FAIL: TestSnapshotter (0.00s)
    snapshotter.go:116: snapshot second differs:
         [
          2,
          {
        -  Foo: "Bar",
        +  Foo: "Foo",
          },
         ]

Happy testing!

# Functions

GlobalSnapshotMode returns the snapshot mode configured in global state via environment variables and/or command-line arguments.
New creates a new Snapshotter.
No description provided by the author

# Constants

SnapshotModeCheck means a snapshot diff will fail the test, and the snapshot will not be updated.
SnapshotModeCheckAndRewrite means a snapshot diff will fail the test, and the snapshot will be updated.
SnapshotModeRewrite means a snapshot diff will be ignored, and the snapshot will be rewritten.
No description provided by the author

# Structs

Snapshotter is a utility for writing snapshot tests.

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author