snapshot

package
v0.0.0-...-6cd82ae Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package snapshot contains adapters that azd uses to create snapshot tests. By default, snapshots are stored under testdata. To change this behavior, customize the config created by snapshot.NewDefaultConfig or snapshot.NewConfig.

Snapshot with default configuration

func TestExample(t *testing.T) {
  result := someFunction()
  snapshot.SnapshotT(t, result)
}

Snapshot with specific file extension

func TestExample(t *testing.T) {
  json := getJson()
  snapshotter := snapshot.NewConfig("json")
  snapshotter.SnapshotT(t, json)
}

To update the snapshots simply set the UPDATE_SNAPSHOTS environment variable and run your tests e.g.

UPDATE_SNAPSHOTS=true go test ./...

Your snapshot files will now have been updated to reflect the current output of your code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConfig

func NewConfig(snapshotExtension string) *cupaloy.Config

NewConfig creates a new snapshot config with the supplied extension name.

This can be useful when the snapshot generated is tied to a specific well-known file extension: json, html, ..., where diff tools will provide better support in diffing the changes.

func NewDefaultConfig

func NewDefaultConfig() *cupaloy.Config

NewDefaultConfig creates the default configuration that azd uses.

func SnapshotT

func SnapshotT(t *testing.T, i ...interface{})

SnapshotT creates a snapshot with the global config, and the current testing.T.

SnapshotT compares the given variable to its previous value stored on the filesystem. test.Fatal is called with the diff if the snapshots do not match, or if a new snapshot was created.

SnapshotT determines the snapshot file automatically from the name of the test. As a result it can be called at most once per test.

If you want to call Snapshot multiple times in a test, collect the values and call Snapshot with all values at once.

To update the snapshots simply set the UPDATE_SNAPSHOTS environment variable and run your tests e.g.

UPDATE_SNAPSHOTS=true go test ./...

Types

This section is empty.

Jump to

Keyboard shortcuts

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