v1

package
v0.0.0-...-e31cc13 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Global = NewDefaultConfig()

Global is the Config instance

Functions

func DeepEqualsIgnoreValues

func DeepEqualsIgnoreValues(obj1, obj2 interface{}) bool

DeepEqualsIgnoreValues compares two objects and returns true if they are equal WITHOUT comparing values.

func Snapshot

func Snapshot(i ...interface{}) error

Snapshot calls Snapshotter.Snapshot with the global config.

func SnapshotMulti

func SnapshotMulti(snapshotID string, i ...interface{}) error

SnapshotMulti calls Snapshotter.SnapshotMulti with the global config.

func SnapshotT

func SnapshotT(t TestingT, i ...interface{})

SnapshotT calls Snapshotter.SnapshotT with the global config.

func SnapshotWithName

func SnapshotWithName(snapshotName string, i ...interface{}) error

SnapshotWithName calls Snapshotter.SnapshotWithName with the global config.

Types

type Config

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

Config provides the same snapshotting functions with additional configuration capabilities.

func New

func New(configurators ...Configurator) *Config

New constructs a new, configured instance of v1 using the given Configurators applied to the default config.

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig returns a new Config instance initialised with the same options as the original Global instance (i.e. before any config changes were made to it)

func (*Config) Snapshot

func (c *Config) Snapshot(i ...interface{}) error

Snapshot compares the given variable to its previous value stored on the filesystem. An error containing a diff is returned if the snapshots do not match, or if a new snapshot was created.

Snapshot determines the snapshot file automatically from the name of the calling function. As a result it can be called at most once per function. If you want to call Snapshot multiple times in a function, if possible, instead collect the values and call Snapshot with all values at once. Otherwise see SnapshotMulti.

If using snapshots in tests, prefer the SnapshotT function which fails the test directly, rather than requiring your to remember to check the error.

func (*Config) SnapshotMulti

func (c *Config) SnapshotMulti(snapshotID string, i ...interface{}) error

SnapshotMulti is similar to Snapshot but can be called multiple times from the same function. This is possible by providing a unique id for each snapshot which is appended to the function name to form the snapshot name.

func (*Config) SnapshotT

func (c *Config) SnapshotT(t TestingT, i ...interface{})

SnapshotT compares the given variable to the its previous value stored on the filesystem. The current test is failed (with error containing a diff) if the values do not match, or if a new snapshot was created.

SnapshotT determines the snapshot file automatically from the name of the test (using the t.Name() function). As a result, SnapshotT can be called at most once per test. If you want to call SnapshotT multiple times in a test, if possible, instead collect the values and call SnapshotT with all values at once. Alternatively, use sub-tests and call SnapshotT once in each.

If using snapshots in tests, SnapshotT is preferred over Snapshot and SnapshotMulti.

func (*Config) SnapshotWithName

func (c *Config) SnapshotWithName(snapshotName string, i ...interface{}) error

SnapshotWithName is similar to SnapshotMulti without appending the function name. It is useful when you need full control of the snapshot filename.

func (*Config) WithOptions

func (c *Config) WithOptions(configurators ...Configurator) *Config

WithOptions returns a copy of an existing Config with additional Configurators applied. This can be used to apply a different option for a single call e.g.

snapshotter.WithOptions(v1.SnapshotSubdirectory("testdata")).SnapshotT(t, result)

Or to modify the Global Config e.g.

v1.Global = v1.Global.WithOptions(v1.SnapshotSubdirectory("testdata"))

type Configurator

type Configurator func(*Config)

Configurator is a functional option that can be passed

func CreateNewAutomatically

func CreateNewAutomatically(createNewAutomatically bool) Configurator

CreateNewAutomatically controls whether snapshots should be automatically created if no matching snapshot already exists. Default: true

func DiffSnapshots

func DiffSnapshots(differ func(previous, current string) string) Configurator

DiffSnapshots allows you to change the diffing function used to display the difference between the previous snapshot and the current. Default: Internal differ using difflib

func EnvVariableName

func EnvVariableName(name string) Configurator

EnvVariableName can be used to customize the environment variable that determines whether snapshots should be updated e.g. Will create an instance where snapshots will be updated if the UPDATE environment variable is set. Default: UPDATE_SNAPSHOTS

func FailOnUpdate

func FailOnUpdate(failOnUpdate bool) Configurator

FailOnUpdate controls whether tests should be failed when snapshots are updated. By default this is true to prevent snapshots being accidentally updated in CI. Default: true

func FatalOnMismatch

func FatalOnMismatch(fatalOnMismatch bool) Configurator

FatalOnMismatch controls whether failed tests should fail using t.Fatal which should immediately stop any remaining tests. Will use t.Error on false. Default: false

func ShouldUpdate

func ShouldUpdate(f func() bool) Configurator

ShouldUpdate can be used to provide custom logic to decide whether or not to update a snapshot

func SnapshotFileExtension

func SnapshotFileExtension(snapshotFileExtension string) Configurator

SnapshotFileExtension allows you to change the extension of the snapshot files that are written. E.g. if you're snapshotting HTML then adding SnapshotFileExtension(".html") will allow for more easier viewing of snapshots. Default: "", no extension is added.

func SnapshotSubdirectory

func SnapshotSubdirectory(name string) Configurator

SnapshotSubdirectory can be used to customize the location that snapshots are stored in. Will create an instance where snapshots are stored in the "testdata" folder Default: .snapshots

func UseStringerMethods

func UseStringerMethods(useStringerMethods bool) Configurator

UseStringerMethods invoke String() or Error() methods when available rather than dumping the object. This should probably be disabled by default but is not for backwards compatibility reasons. Default: true

type TestingT

type TestingT interface {
	Helper()
	Failed() bool
	Error(args ...interface{})
	Fatal(args ...interface{})
	Name() string
}

TestingT is a subset of the interface testing.TB allowing it to be mocked in tests.

Jump to

Keyboard shortcuts

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