Categorygithub.com/djrollins/snapshot
modulepackage
0.2.1
Repository: https://github.com/djrollins/snapshot.git
Documentation: pkg.go.dev

# README

snapshot - Snapshot test utilities for go

snapshot provides a set of utility functions for creating and loading snapshot files for using snapshot tests.

Install

go get -u github.com/djrollins/snapshot

Output Snapshot Example

package example

import (
	"testing"

	"github.com/djrollins/snapshot"
)

func ScanSum(in []int) (out []int) {
	sum := 0
	for i := range in {
		sum += i
		out = append(out, sum)
	}
	return
}

func TestScanPlus(t *testing.T) {
	nums := make([]int, 1000)
	for i := range nums {
		nums[i] = i
	}
	scan := ScanSum(nums)
	asJSON, err := snapshot.AsJSON(scan)
	if err != nil {
		t.Fatalf("failed to encode scan result to JSON")
	}
	ok, msg := snapshot.Match(t, asJSON)
	if !ok {
		t.Fatalf("scan did not match snapshot: %v", msg)
	}
}

Input Snapshot Example

package example

import (
	"io"
	"math/rand"
	"strings"
	"testing"
	"time"

	"github.com/djrollins/snapshot"
)

var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

// EAR == "ExpensiveAsyncronousRandom" opertion provided by an external system,
// which also exposes a version number for their library.
const EARVersion = "v0.2.0"

func ExpensiveAsyncronousRandomOperation() (out io.Reader, err error) {
	out, in := io.Pipe()
	go func() {
		for i := 1; i < 1000; i++ {
			n := rand.Intn(len(letters))
			c := letters[n]
			in.Write([]byte{c})
			time.Sleep(2 * time.Millisecond)
		}
		in.Close()
	}()
	return
}

func TestEAR(t *testing.T) {
	rand.Seed(time.Now().UnixNano())
	// First time this test runs, this will take ~2 seconds. However, the
	// the input data is persisted to disk and reused on subsequent runs. This
	// allows for fast, repeatable tests that require complex input data
	// without having to manually create it. This should be used with custom
	// snapshot names based on versions so that a new snapshot is created
	// and breaking changes can be detected between versions.
	input := snapshot.GetTestInput(t,
		snapshot.WithCreateSnapshot(ExpensiveAsyncronousRandomOperation),
		snapshot.WithSnapshotName("ear_"+EARVersion),
	)
	buf := new(strings.Builder)
	_, _ = io.Copy(buf, input)

	upper := strings.ToUpper(buf.String())

	// Match will pass on the first run, as there is no snapshot recorded,
	// however it will ensure that future runs result in the same data,
	// until the snapshot is deleted.
	ok, msg := snapshot.Match(t, strings.NewReader(upper))
	if !ok {
		t.Fatalf("unexpected output: %v", msg)
	}
}

# Packages

No description provided by the author

# Functions

AsJSON marshals i to the io.Reader.
GetTestInput gets loads the input snapshot for a particular test case.
Match loads the output snapshot for a particular test case.
NopReaderNormaliser is the default ReaderNormaliser.
StringComparator reads expected and actual into strings and performs an equality check.
StringDiffComparator reads expected and actual into strings compares them using cmp.Diff from the go-cmp library.
WithComparator overrides the default comparator with a custom function.
WithCreateSnapshot provides a SnapshotCreator function to specify the data for the test when no snapshot file exists.
WithCreateSnapshotAsJSON configures GetTestInput to use AsJSON as the CreateSnapshot and sets the file extension to ".json".
WithCreateSnapshotFromReader creates SnapshotCreator func from a reader.
WithReaderNormaliser provides a ReaderNormaliser function to apply to the actual and expected io.Readers before comparison.
WithSnapshotFileExtension overrides the file extension for the resulting snapshot file.
WithSnapshotFilename overrides snapshot name and file extension of the resulting snapshot file.
WithSnapshotName overrides the snapshot name.

# Structs

GetTestInputOptions are the set of options to configure the behaviour of GetTestInput.
MatchOptions are the set of options to configure the behaviour of Match.

# Interfaces

GetTestInputOption may be an argument to GetTestInput in order to change GetTestInputOptions.
MatchOption may be an argument to Match in order to change MatchOptions.
No description provided by the author

# Type aliases

A Comparator can be used to override the default snapshot comparison.
GetTestInputOptionFunc applies a func to the GetTestInputOptions defaults.
WithMatchOption applies f to the MatchOptions defaults.
A ReaderNormaliser is a function that takes an io.Reader and returns a new io.Reader after some processing.
A SnapshotCreator is a function that can be provided to GetTestInput which will be used used in the case where an input snapshot file does not exist.