snaps

package
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dir

func Dir(dir string) func(*Config)

Dir Specify folder name where snapshots are stored

default: __snapshots__

Accepts absolute paths

func Ext

func Ext(ext string) func(*Config)

Ext 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

func Filename(name string) func(*Config)

Filename Specify folder name where snapshots are stored

default: __snapshots__

this doesn't change the file extension see `snap.Ext`

func MatchJSON

func MatchJSON(t TestingT, input any, matchers ...matchers.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

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

func Skip(t TestingT, args ...any)

Skip Wrapper of testing.Skip

Keeps track which snapshots are getting skipped and not marked as obsolete.

func SkipNow

func SkipNow(t TestingT)

SkipNow Wrapper of testing.SkipNow

Keeps track which snapshots are getting skipped and not marked as obsolete.

func Skipf

func Skipf(t TestingT, format string, args ...any)

Skipf Wrapper of testing.Skipf

Keeps track which snapshots are getting skipped and not marked as obsolete.

func SortProperties added in v0.0.10

func SortProperties() func(*Config)

SortProperties sort properties in snapshots

default: false

func Update

func Update(u bool) func(*Config)

Update determines whether to update snapshots or not

It respects if running on CI.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func WithConfig

func WithConfig(args ...func(*Config)) *Config

WithConfig Create snaps with configuration

snaps.WithConfig(snaps.Filename("my_test")).MatchSnapshot(t, "hello world")

func (*Config) Extension

func (c *Config) Extension() string

func (*Config) Filename

func (c *Config) Filename() string

func (*Config) MatchJSON

func (c *Config) MatchJSON(t TestingT, input any, matchers ...matchers.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 (*Config) MatchSnapshot

func (c *Config) 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 (*Config) MatchStandaloneSnapshot

func (c *Config) 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 (*Config) SnapsDir

func (c *Config) SnapsDir() string

func (*Config) SortProperties added in v0.0.10

func (c *Config) SortProperties() bool

func (*Config) Update

func (c *Config) Update() *bool

type TestingT

type TestingT interface {
	Helper()
	Skip(...any)
	Skipf(string, ...any)
	SkipNow()
	Name() string
	Error(...any)
	Log(...any)
	Cleanup(func())
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL