Documentation
¶
Index ¶
- func Clean(m *testing.M, opts ...CleanOpts) (bool, error)
- func Dir(dir string) func(*Config)
- func Ext(ext string) func(*Config)
- func Filename(name string) func(*Config)
- func Inline(s string) inlineSnapshot
- func JSON(json JSONConfig) func(*Config)
- func MatchInlineSnapshot(t testingT, received any, inlineSnap inlineSnapshot)
- func MatchJSON(t testingT, input any, matchers ...match.JSONMatcher)
- func MatchSnapshot(t testingT, values ...any)
- func MatchStandaloneJSON(t testingT, input any, matchers ...match.JSONMatcher)
- func MatchStandaloneSnapshot(t testingT, input any)
- func MatchStandaloneYAML(t testingT, input any, matchers ...match.YAMLMatcher)
- func MatchYAML(t testingT, input any, matchers ...match.YAMLMatcher)
- func Skip(t testingT, args ...any)
- func SkipNow(t testingT)
- func Skipf(t testingT, format string, args ...any)
- func Update(u bool) func(*Config)
- type CleanOpts
- type Config
- func (c *Config) MatchInlineSnapshot(t testingT, received any, inlineSnap inlineSnapshot)
- func (c *Config) MatchJSON(t testingT, input any, matchers ...match.JSONMatcher)
- func (c *Config) MatchSnapshot(t testingT, values ...any)
- func (c *Config) MatchStandaloneJSON(t testingT, input any, matchers ...match.JSONMatcher)
- func (c *Config) MatchStandaloneSnapshot(t testingT, input any)
- func (c *Config) MatchStandaloneYAML(t testingT, input any, matchers ...match.YAMLMatcher)
- func (c *Config) MatchYAML(t testingT, input any, matchers ...match.YAMLMatcher)
- type JSONConfig
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
dirty, err := snaps.Clean(m)
if err != nil {
fmt.Println("Error cleaning snaps:", err)
os.Exit(1)
}
if dirty {
fmt.Println("Some snapshots were outdated.")
os.Exit(1)
}
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
dirty, err := snaps.Clean(m, snaps.CleanOpts{Sort: true})
if err != nil {
fmt.Println("Error cleaning snaps:", err)
os.Exit(1)
}
if dirty {
fmt.Println("Some snapshots were outdated.")
os.Exit(1)
}
os.Exit(v)
}
func Dir ¶ added in v0.4.5
Specify folder name where snapshots are stored
default: __snapshots__
Accepts absolute paths
func Ext ¶ added in v0.5.6
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
Specify snapshot file name
default: test's filename
this doesn't change the file extension see `snap.Ext`
func Inline ¶ added in v0.5.16
func Inline(s string) inlineSnapshot
Inline representation of snapshot
func JSON ¶ added in v0.5.11
func JSON(json JSONConfig) func(*Config)
Specify json format configuration
default: see defaultPrettyJSONOptions for default json config
func MatchInlineSnapshot ¶ added in v0.5.16
func MatchInlineSnapshot(t testingT, received any, inlineSnap inlineSnapshot)
MatchInlineSnapshot compares a test value against an expected "inline snapshot" value.
Usage:
On the first run, call with nil as the expected value: MatchInlineSnapshot(t, "mysnapshot", nil) This will record the current value as the snapshot.
On subsequent runs, call with the snapshot value: MatchInlineSnapshot(t, "mysnapshot", snaps.Inline("mysnapshot")) This will verify that the value matches the stored snapshot.
An "inline snapshot" is a literal value stored directly in your test code, making it easy to update and review expected outputs.
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`.
snaps.MatchJSON(t, `{"user":"mock-user","age":10,"email":"[email protected]"}`)
snaps.MatchJSON(t, []byte(`{"user":"mock-user","age":10,"email":"[email protected]"}`))
snaps.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.
snaps.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 MatchStandaloneJSON ¶ added in v0.5.8
func MatchStandaloneJSON(t testingT, input any, matchers ...match.JSONMatcher)
MatchStandaloneJSON 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`.
snaps.MatchStandaloneJSON(t, `{"user":"mock-user","age":10,"email":"[email protected]"}`)
snaps.MatchStandaloneJSON(t, []byte(`{"user":"mock-user","age":10,"email":"[email protected]"}`))
snaps.MatchStandaloneJSON(t, User{10, "mock-email"})
MatchStandaloneJSON 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.
snaps.MatchStandaloneJSON(t, User{Created: time.Now(), Email: "mock-email"}, match.Any("created"))
MatchStandaloneJSON creates one snapshot file per call.
You can call MatchStandaloneJSON multiple times inside a test. It will create multiple snapshot files at `__snapshots__` folder by default.
func MatchStandaloneSnapshot ¶ added in v0.5.6
func MatchStandaloneSnapshot(t testingT, input any)
MatchStandaloneSnapshot verifies the input 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 MatchStandaloneYAML ¶ added in v0.5.13
func MatchStandaloneYAML(t testingT, input any, matchers ...match.YAMLMatcher)
MatchStandaloneYAML verifies the input matches the most recent snap file. Input can be a valid yaml string or []byte or whatever value can be passed successfully on `yaml.Marshal`.
snaps.MatchStandaloneYAML(t, "user: \"mock-user\"\nage: 10\nemail: [email protected]") snaps.MatchStandaloneYAML(t, []byte("user: \"mock-user\"\nage: 10\nemail: [email protected]")) snaps.MatchStandaloneYAML(t, User{10, "mock-email"})
MatchStandaloneYAML 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.
snaps.MatchStandaloneYAML(t, User{Created: time.Now(), Email: "mock-email"}, match.Any("$.created"))
MatchStandaloneYAML creates one snapshot file per call.
You can call MatchStandaloneYAML multiple times inside a test. It will create multiple snapshot files at `__snapshots__` folder by default.
func MatchYAML ¶ added in v0.5.8
func MatchYAML(t testingT, input any, matchers ...match.YAMLMatcher)
MatchYAML verifies the input matches the most recent snap file. Input can be a valid yaml string or []byte or whatever value can be passed successfully on `yaml.Marshal`.
snaps.MatchYAML(t, "user: \"mock-user\"\nage: 10\nemail: [email protected]") snaps.MatchYAML(t, []byte("user: \"mock-user\"\nage: 10\nemail: [email protected]")) snaps.MatchYAML(t, User{10, "mock-email"})
MatchYAML 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.
snaps.MatchYAML(t, User{Created: time.Now(), Email: "mock-email"}, match.Any("$.created"))
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.
Types ¶
type CleanOpts ¶ added in v0.4.12
type CleanOpts struct {
// If set to true, `go-snaps` will sort the snapshots
Sort bool
}
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
func WithConfig ¶ added in v0.4.5
Create snaps with configuration
e.g snaps.WithConfig(snaps.Filename("my_test")).MatchSnapshot(t, "hello world")
func (*Config) MatchInlineSnapshot ¶ added in v0.5.16
MatchInlineSnapshot compares a test value against an expected "inline snapshot" value.
Usage:
On the first run, call with nil as the expected value: MatchInlineSnapshot(t, "mysnapshot", nil) This will record the current value as the snapshot.
On subsequent runs, call with the snapshot value: MatchInlineSnapshot(t, "mysnapshot", snaps.Inline("mysnapshot")) This will verify that the value matches the stored snapshot.
An "inline snapshot" is a literal value stored directly in your test code, making it easy to update and review expected outputs.
func (*Config) MatchJSON ¶ added in v0.5.7
func (c *Config) 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`.
snaps.MatchJSON(t, `{"user":"mock-user","age":10,"email":"[email protected]"}`)
snaps.MatchJSON(t, []byte(`{"user":"mock-user","age":10,"email":"[email protected]"}`))
snaps.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.
snaps.MatchJSON(t, User{Created: time.Now(), Email: "mock-email"}, match.Any("created"))
func (*Config) MatchSnapshot ¶
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 (*Config) MatchStandaloneJSON ¶ added in v0.5.8
func (c *Config) MatchStandaloneJSON(t testingT, input any, matchers ...match.JSONMatcher)
MatchStandaloneJSON 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`.
snaps.MatchStandaloneJSON(t, `{"user":"mock-user","age":10,"email":"[email protected]"}`)
snaps.MatchStandaloneJSON(t, []byte(`{"user":"mock-user","age":10,"email":"[email protected]"}`))
snaps.MatchStandaloneJSON(t, User{10, "mock-email"})
MatchStandaloneJSON 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.
snaps.MatchStandaloneJSON(t, User{Created: time.Now(), Email: "mock-email"}, match.Any("created"))
MatchStandaloneJSON creates one snapshot file per call.
You can call MatchStandaloneJSON multiple times inside a test. It will create multiple snapshot files at `__snapshots__` folder by default.
func (*Config) MatchStandaloneSnapshot ¶ added in v0.5.7
MatchStandaloneSnapshot verifies the input 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 (*Config) MatchStandaloneYAML ¶ added in v0.5.13
func (c *Config) MatchStandaloneYAML(t testingT, input any, matchers ...match.YAMLMatcher)
MatchStandaloneYAML verifies the input matches the most recent snap file. Input can be a valid yaml string or []byte or whatever value can be passed successfully on `yaml.Marshal`.
snaps.MatchStandaloneYAML(t, "user: \"mock-user\"\nage: 10\nemail: [email protected]") snaps.MatchStandaloneYAML(t, []byte("user: \"mock-user\"\nage: 10\nemail: [email protected]")) snaps.MatchStandaloneYAML(t, User{10, "mock-email"})
MatchStandaloneYAML 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.
snaps.MatchStandaloneYAML(t, User{Created: time.Now(), Email: "mock-email"}, match.Any("$.created"))
MatchStandaloneYAML creates one snapshot file per call.
You can call MatchStandaloneYAML multiple times inside a test. It will create multiple snapshot files at `__snapshots__` folder by default.
func (*Config) MatchYAML ¶ added in v0.5.8
func (c *Config) MatchYAML(t testingT, input any, matchers ...match.YAMLMatcher)
MatchYAML verifies the input matches the most recent snap file. Input can be a valid yaml string or []byte or whatever value can be passed successfully on `yaml.Marshal`.
snaps.MatchYAML(t, "user: \"mock-user\"\nage: 10\nemail: [email protected]") snaps.MatchYAML(t, []byte("user: \"mock-user\"\nage: 10\nemail: [email protected]")) snaps.MatchYAML(t, User{10, "mock-email"})
MatchYAML 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.
snaps.MatchYAML(t, User{Created: time.Now(), Email: "mock-email"}, match.Any("$.created"))
type JSONConfig ¶ added in v0.5.11
type JSONConfig struct {
// Width is a max column width for single line arrays
// Default: see defaultPrettyJSONOptions.Width for detail
Width int
// Indent is the nested indentation
// Default: see defaultPrettyJSONOptions.Indent for detail
Indent string
// SortKeys will sort the keys alphabetically
// Default: see defaultPrettyJSONOptions.SortKeys for detail
SortKeys bool
}