snapshot

package module
v0.0.0-...-ebdc4b3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: MIT Imports: 11 Imported by: 0

README

Snapshot

Package snapshot implements snapshot testing for Go.

It works by modifying the test file itself during execution.

Installation

go get github.com/aarol/snapshot

Example

  • Step 1: Call the MatchesInline function inside your tests:
package snapshot_test

import (
	"testing"

	"github.com/aarol/snapshot"
)

func TestSnapshot(t *testing.T) {
	val := 1
	snapshot.MatchesInline(t, val)

	val2 := "123"
	snapshot.MatchesInline(t, val2)
}
  • Step 2: Run the tests with go test and observe the added parameters:

func TestSnapshot(t *testing.T) {
	val := 1
	snapshot.MatchesInline(t, val, 1)

	val2 := "123"
	snapshot.MatchesInline(t, val2, "123")
}

Now, any future values of val or val2 will be compared against the hard-coded value. If it were to change, the test would error:

> go test .

--- FAIL: TestSnapshot (0.00s)
    snapshot_test.go:11: snapshot doesn't match: want 1, got 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchesInline

func MatchesInline(t T, actual any, expected ...string)

func MatchesPretty

func MatchesPretty(t T, actual any, expected ...string)

Types

type App

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

func (*App) Run

func (app *App) Run(d int) error

type T

type T interface {
	Helper()
	Errorf(format string, args ...any)
}

Jump to

Keyboard shortcuts

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