Skip to content

jellevandenhooff/snap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snap: Source-based snapshot tests for Go

Go Reference

Snap is a package for automatically updating source-based snapshots tests. A common pattern for Go tests is to store expected outputs in a "golden" file in a testdata/ directory. That is quite convenient for storing and tracking complicated values. But wouldn't it be nice if the expected value was right there in the source code? That is what snap supports.

If you have a test like the following which computes and compares a sha256 hash, it can be annoying to write the correct expected value in the source:

package example_test

import (
    "crypto/sha256"
    "encoding/hex"
    "testing"

    "github.com/jellevandenhooff/snap"
)

func complicated(s string) string {
    b := sha256.Sum256([]byte(s))
    return hex.EncodeToString(b[:16])
}

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

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

If you test this with "go test" you get an error saying the expected value is wrong:

> go test
=== RUN   TestSnapshot
    example_test.go:19: snapshot different; expected "", got "2d5a34155bd3feb0728c3198c41250db"
--- FAIL: TestSnapshot (0.00s)
...

To fix the test we can either copy and paste this expected string... or we can update the snapshots using snap:

> go test -update-snapshots
PASS

Now the test has been updated to include:

expected := snap.Source(`2d5a34155bd3feb0728c3198c41250db`)

And tests pass:

> go test
PASS

In other languages

This package was inspired by source-based snapshot tests in other languages. In Rust there is expect-test and in Ocaml there is "%expect".

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages