Documentation ¶
Overview ¶
Package snapshot provides utility routines for image snapshot testing.
With the package, you can ensure that an image look like the same as before: that there is no visual regression.
A PNG file is generated when running a test with Match function, and used to compare with images generated in subsequent tests.
import snap "github.com/midorimici/goimagesnapshot" func TestSnapshot(t *testing.T) { t.Run("renders unchanged", func(t *testing.T) { snap.Match(t, img) }) }
To update snapshots, set UPDATE_SNAPSHOTS environment variable when running tests.
// Updates snapshots with the same name, but does not delete obsolete files. UPDATE_SNAPSHOTS=1 go test ./... // Removes the snapshot directory at first (i.e. obsolete files are deleted) and then snapshots are regenerated. UPDATE_SNAPSHOTS=2 go test ./...
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithDirectory ¶
func WithDirectory(d string) option.MatcherOption
WithDirectory returns a snapshot matcher option which specifies the directory name where the output snapshot images are put.
The default value is "__snapshots__".
func WithName ¶
func WithName(n string) option.SnapshotOption
WithName returns a snapshot option which specifies the output snapshot file name.
A file name uses the test name by default.
func WithOnlyPixelComparison ¶
func WithOnlyPixelComparison() option.SnapshotOption
WithOnlyPixelComparison returns a snapshot option which specifies to compare only image pixels between two snapshots, which generally makes the test more strict.
By default, the matcher first compares the two image byte slices. If they differ, then their image pixels are tested one by one.
Specifying this option skips the first byte slice comparison.
func WithThreshold ¶
func WithThreshold(t float64) option.SnapshotOption
WithThreshold returns a snapshot option which specifies a value which a test fails when the differences by percent in compared images exceeds. For example, when compared images have 0.11% of differences, a test fails with threshold of 0.1, whereas it passes with threshold of 0.2.
The default value is 0, i.e. two images must match perfectly.
Types ¶
type Matcher ¶
func NewMatcher ¶
func NewMatcher(opts ...option.MatcherOption) Matcher
NewMatcher returns a new matcher with matcher options.