Giter Club home page Giter Club logo

snapshot's Introduction

Snapshot

Snapshot is a simple library for snapshot testing in Go. I find it useful for testing complex command-line applications when I'm interested in finding regressions to the UI. When you run a test for the first time, the output is written to a snapshot file. Subsequent tests ensure that test output matches your snapshot.

package example

import (
	"testing"

	"github.com/BTBurke/snapshot"
)

func TestOutput(t *testing.T) {

	// This output matches the testoutput.snap in the __snapshots__ directory
	output := []byte("This is my UI output\nAnd it can be complex")
	snapshot.Assert(t, output)

	// If I assert something different, I'll get a test failure and a diff of what changed
	output = []byte("This is my UI output\nAnd it can be *very* complex")
	snapshot.Assert(t, output)

	// Output:
	// Snapshot test failed for: TestOutput.  Diff:
	//
	// --- Expected
	// +++ Received
	// @@ -1,2 +1,2 @@
	// This is my UI output
	// -And it can be complex
	// +And it can be *very* complex
}

Updating snapshots

When you make UI changes and want to create new snapshots, set the environment variable UPDATE_SNAPSHOTS before running your tests.

UPDATE_SNAPSHOTS=true go test -v -cover

Setting a custom snapshot directory and context

By default, snapshots are saved in the __snapshots__ directory relative to the working directory where you run go test. To change this behavior, you can create your own configuration prior to running your snapshot assertions. The snapshot directory should be the full path. You can also change the number of lines of context that are shown around your diffs.

import (
	"testing"

	"github.com/BTBurke/snapshot"
)

func TestOutput(t *testing.T) {

    mycfg, _ := snapshot.New(SnapDirectory("/snaps"), ContextLines(0))

	// This output matches the testoutput.snap in the /snaps directory
	output := []byte("This is my UI output\nAnd it can be complex")
	mycfg.Assert(t, output)

	// If I assert something different, I'll get a test failure and a diff of what changed
	output = []byte("This is my UI output\nAnd it can be *very* complex")
	mycfg.Assert(t, output)

	// Output:
	// Snapshot test failed for: TestOutput.  Diff:
	//
	// --- Expected
	// +++ Received
	// @@ -1,2 +1,2 @@
	// -And it can be complex
	// +And it can be *very* complex
}

snapshot's People

Contributors

btburke avatar yodra avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.