snap

package module
v0.0.0-...-2789f5d Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package snap supports source-based snapshot testing. Package snap maintains expected or "golden" values in _test.go files and can update the expected value by rewriting the source.

To make a snapshot test, create a snapshot with Source and test the snapshot with a Check function such as CheckString. Then, call Run in a TestMain function and run tests with "go test -update-snapshots" to update snapshots.

As an example, this is a complete test file with Source, CheckString, and Run. The snapshot is out of date: It should mention "complicated value" but instead it is "old".

package example_test

import (
   "testing"
   "github.com/jellevandenhooff/snap"
)

func TestSnapshot(t *testing.T) {
	magic := "complicated value"
	snap.CheckString(t, magic, snap.Source("old"))
}

func TestMain(m *testing.M) {
	snap.Run(m)
}

Running "go test" will fail because the snapshot is out of date. Running "go test -update-snapshots" will update the snapshot in the file to "snap.Source(`complicated value`)" and afterwards tests will pass.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckJSON

func CheckJSON(t *testing.T, actual any, expected *Snapshot)

CompareString compares a JSON-marshaled with a snapshot.

func CheckString

func CheckString(t *testing.T, actual string, expected *Snapshot)

CheckString compares a string with a snapshot.

func Run

func Run(m *testing.M)

Run should be called from TestMain (see testing.M) to run tests and optionally update snapshots with the "go test -update-snapshots" flag.

Any snapshots in skipped tests will be kept as-is.

Types

type Snapshot

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

A Snapshot is an expected value in a test created by calling Source. See Source on how to use Snapshots.

func Source

func Source(input string) *Snapshot

Source creates a automatically-updated Snapshot.

A Snapshot must be passed to a Check function like CheckJSON to be tested. Each Snapshot can be used only once.

The argument to Source can be automatically updated against actual values using the "go test -update-snapshots" flag; see Run.

Only one call to Source per line is supported.

Jump to

Keyboard shortcuts

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