Documentation
¶
Overview ¶
Package snapshot is a Snapshot testing library for Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Formatter ¶ added in v0.7.0
type Formatter interface {
// Format returns a formatted version of 'value' as a raw byte slice, these
// bytes are interpreted as the snapshot and will be written and read from disk
// during snapshot comparisons.
Format(value any) ([]byte, error)
// Ext returns the file extension for the snapshot, including the dot
// e.g. ".custom".
Ext() string
}
Formatter is an interface describing something capable of producing a snapshot.
func InstaFormatter ¶ added in v0.8.0
InstaFormatter returns a Formatter that produces snapshots in the insta yaml format.
It takes a description for the snapshot.
func TextFormatter ¶ added in v0.8.0
func TextFormatter() Formatter
TextFormatter returns a Formatter that produces snapshots by simply dumping the value as plain text.
type Option ¶
Option is a functional option for configuring a snapshot test Runner.
func Clean ¶
Clean is an Option that tells snapshot to erase the snapshots directory for the given test before it runs. This is particularly useful if you've renamed or restructured your tests since the snapshots were last generated to remove all unused snapshots.
Typically, you'll want the value of this option to be set from an environment variable or a test flag so that it only happens when explicitly requested, as like Update, fresh snapshots will always pass the tests.
func Color ¶
Color is an Option that tells snapshot whether it can use ANSI terminal colors when rendering the diff.
By default snapshot will auto-detect whether to use colour based on things like $NO_COLOR, $FORCE_COLOR, whether os.Stdout is a terminal etc.
Passing this option will override default detection and set the provided value.
func Description ¶ added in v0.7.0
Description is an Option that attaches a brief, human-readable description that may be serialised with the snapshot depending on the format.
func Filter ¶
Filter is an Option that configures a filter that is applied to a snapshot prior to saving to disc.
Filters can be used to ensure deterministic snapshots given non-deterministic data.
A motivating example would be if your snapshot contained a UUID that was generated each time, your snapshot would always fail.
Instead you might add a filter:
snapshot.New(t, snapshot.Filter("(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", "[UUID]"))
Now any match of this regex anywhere in your snapshot will be replaced by the literal string "[UUID]".
Inside replacement, '$' may be used to refer to capture groups. For example:
snapshot.Filter(`\\([\w\d]|\.)`, "/$1")
Replaces windows style paths with a unix style path with the same information, e.g.
some\windows\path.txt
Becomes:
some/windows/path.txt
Filters use regexp.ReplaceAll underneath so in general the behaviour is as documented there, see also regexp.Expand for documentation on how '$' may be used.
func Update ¶
Update is an Option that tells snapshot whether to automatically update the stored snapshots with the new value from each test.
Typically, you'll want the value of this option to be set from an environment variable or a test flag so that you can inspect the diffs prior to deciding that the changes are expected, and therefore the snapshots should be updated.
func WithFormatter ¶ added in v0.7.0
WithFormatter sets the Formatter that snapshots will be serialised and deserialised with.
The default implementation of a Formatter is one that serialises snapshots in the insta yaml format.
In the future we may provide alternative formatters, but in the meantime you can implement your own Formatter and pass it here.
type Runner ¶ added in v0.7.0
type Runner struct {
// contains filtered or unexported fields
}
Runner is the snapshot testing runner.
It holds configuration and state for the snapshot test in question.
func New ¶
New initialises a new snapshot test Runner.
The behaviour of the snapshot test can be configured by passing a number of Option.
func (Runner) Path ¶ added in v0.7.0
Path returns the path that a snapshot would be saved at for any given test.
func (Runner) Snap ¶ added in v0.7.0
Snap takes a snapshot of a value and compares it against the previous snapshot stored under testdata/snapshots using the name of the test as the filepath.
If there is a previous snapshot saved for this test, the newly generated snapshot is compared with the one on disk. If the two snapshots differ, the test is failed and a rich diff is shown for comparison.
If the newly generated snapshot and the one previously saved are the same, the test passes.
Likewise if there was no previous snapshot, the new one is written to disk and the test passes.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
format
Package format provides a variety of snapshot serialisation formats.
|
Package format provides a variety of snapshot serialisation formats. |
|
format/insta
Package insta implements the .snap format for snapshots, popularised by the rust insta crate.
|
Package insta implements the .snap format for snapshots, popularised by the rust insta crate. |
|
format/text
Package text provides a simple text formatter for snapshots.
|
Package text provides a simple text formatter for snapshots. |