Categorygithub.com/plouc/gosnap
modulepackage
0.0.0-20180714070049-61403c2f226e
Repository: https://github.com/plouc/gosnap.git
Documentation: pkg.go.dev

# README

gosnap

Build Status GoDoc GitHub license Go Report Card GitHub issues

gosnap is a go package to perform snapshot testing.

Snapshot testing is a convenient way to test large outputs with ease, this package was initially created to test the go-gitlab-client CLI.

Install

go get github.com/plouc/gosnap

Usage

gosnap requires a context to run, from which you can create snapshots.

package whatever

import (
    "testing"
    "github.com/plouc/gosnap"
)

func TestSomething(t *testing.T) {
    // creates a new context
    // snapshots will be stored inside the `snapshots` directory
    ctx := gosnap.NewContext(t, "snapshots")

    // creates a new snapshot
    // it will be stored in `snapshots/my_first_snapshot.snap`
    s := ctx.NewSnapshot("my_first_snapshot")
    
    actual := DoSomethingWhichReturnsString()
    
    // this will load the snapshot content and check it matches `actual`
    s.AssertString(actual)
}

This will check that actual matches current snapshot (./snapshots/my_first_snapshot.snap) content.

The first time you run your tests, the snapshot will be created automatically, then if the current result does not match snapshot's content, you'll have to update it, you can add a command-line flag to the go test command to do so:

# will update all stale snapshots
go test -v ./... -args -update all

# will just update snapshot whose name is `my_snapshot`
go test -v ./... -args -update my_snapshot

For complete usage of gosnap, see the full package docs.

# Functions

NewContext creates a new snapshot testing context By default the `AutoUpdate` will be enabled according to `-update` or `-u` command-line flag, if it equals `all` `AutoUpdate` will be enabled.
StringsDiff returns a pretty diff between given strings.

# Structs

Context is used to isolate snapshot testing for given options.
Snapshot represents a snapshot file.