Documentation
¶
Index ¶
- func Clean(m *testing.M, opts ...CleanOpts)
- func Dir(dir string) func(*config)
- func Ext(ext string) func(*config)
- func Filename(name string) func(*config)
- func MatchJSON(t testingT, input any, matchers ...match.JSONMatcher)
- func MatchSnapshot(t testingT, values ...any)
- func MatchStandaloneSnapshot(t testingT, value any)
- func Skip(t testingT, args ...any)
- func SkipNow(t testingT)
- func Skipf(t testingT, format string, args ...any)
- func Update(u bool) func(*config)
- func WithConfig(args ...func(*config)) *config
- type CleanOpts
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clean ¶ added in v0.1.3
Clean runs checks for identifying obsolete snapshots and prints a Test Summary.
Must be called in a TestMain
func TestMain(m *testing.M) {
v := m.Run()
// After all tests have run `go-snaps` can check for unused snapshots
snaps.Clean(m)
os.Exit(v)
}
Clean also supports options for sorting the snapshots
func TestMain(m *testing.M) {
v := m.Run()
// After all tests have run `go-snaps` will sort snapshots
snaps.Clean(m, snaps.CleanOpts{Sort: true})
os.Exit(v)
}
func Dir ¶ added in v0.4.5
func Dir(dir string) func(*config)
Specify folder name where snapshots are stored
default: __snapshots__
Accepts absolute paths
func Ext ¶ added in v0.5.6
func Ext(ext string) func(*config)
Specify file name extension
default: .snap
Note: even if you specify a different extension the file still contain .snap e.g. if you specify .txt the file will be .snap.txt
func Filename ¶ added in v0.4.5
func Filename(name string) func(*config)
Specify folder name where snapshots are stored
default: __snapshots__
this doesn't change the file extension see `snap.Ext`
func MatchJSON ¶ added in v0.4.1
func MatchJSON(t testingT, input any, matchers ...match.JSONMatcher)
MatchJSON verifies the input matches the most recent snap file. Input can be a valid json string or []byte or whatever value can be passed successfully on `json.Marshal`.
MatchJSON(t, `{"user":"mock-user","age":10,"email":"mock@email.com"}`)
MatchJSON(t, []byte(`{"user":"mock-user","age":10,"email":"mock@email.com"}`))
MatchJSON(t, User{10, "mock-email"})
MatchJSON also supports passing matchers as a third argument. Those matchers can act either as validators or placeholders for data that might change on each invocation e.g. dates.
MatchJSON(t, User{created: time.Now(), email: "mock-email"}, match.Any("created"))
func MatchSnapshot ¶
func MatchSnapshot(t testingT, values ...any)
MatchSnapshot verifies the values match the most recent snap file You can pass multiple values
MatchSnapshot(t, 10, "hello world")
or call MatchSnapshot multiples times inside a test
MatchSnapshot(t, 10) MatchSnapshot(t, "hello world")
The difference is the latter will create multiple entries.
func MatchStandaloneSnapshot ¶ added in v0.5.6
func MatchStandaloneSnapshot(t testingT, value any)
MatchStandaloneSnapshot verifies the value matches the most recent snap file
MatchStandaloneSnapshot(t, "Hello World")
MatchStandaloneSnapshot creates one snapshot file per call.
You can call MatchStandaloneSnapshot multiple times inside a test. It will create multiple snapshot files at `__snapshots__` folder by default.
func Skip ¶ added in v0.1.3
func Skip(t testingT, args ...any)
Wrapper of testing.Skip
Keeps track which snapshots are getting skipped and not marked as obsolete.
func SkipNow ¶ added in v0.1.3
func SkipNow(t testingT)
Wrapper of testing.SkipNow
Keeps track which snapshots are getting skipped and not marked as obsolete.
func Skipf ¶ added in v0.1.3
Wrapper of testing.Skipf
Keeps track which snapshots are getting skipped and not marked as obsolete.
func Update ¶ added in v0.4.11
func Update(u bool) func(*config)
Update determines whether to update snapshots or not
It respects if running on CI.
func WithConfig ¶ added in v0.4.5
func WithConfig(args ...func(*config)) *config
Create snaps with configuration
e.g snaps.WithConfig(snaps.Filename("my_test")).MatchSnapshot(t, "hello world")