snapshot

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2019 License: MIT Imports: 13 Imported by: 0

README

snapshot

Provides basic methods for snapshot testing, inspired by jest's snapshots

This package doesn't aim to be anything more than a simple utility to be used in conjunction with golang's own testing package, or any other testing framework which is also compatible with go test snapshots are auto-generated and managed by this package:

Getting started

  1. Grab the package
go get -u github.com/vancelongwill/snapshot
  1. Use it in tests
import (
  "testing"

  "github.com/VanceLongwill/snapshot"
)

type Something struct {
  MoreThings []string
}

func TestSomething(t *testing.T) {
  something := Something{}
  something.MoreThings = []string{"a", "b", "c"}
  label := "should be something"
  err := snapshot.WithLabel(label).Matches(&something)
  // returns an error with a pretty diff if the snapshot doesnt match
  if err != nil {
    // print the label and the diff
    t.Fatalf("Snapshot didn't match.\n - '%s'\n%s",
      label,
      err.Error())
  }
}
  1. Update failing snapshots
go test ./... -u
Contributing

Contributions are absolutely welcome!

Steps
  • Make sure tests are passing

    make test
    
  • Make sure it passes the linter

    make test
    
  • Open a pull request

Documentation

Overview

Package snapshot provides basic methods for snapshot testing Snapshot tests may be useful for regression testing or unit testing (with proper review) This package doesn't aim to be anything more than a simple utility to be used in conjunction with golang's own testing package, or any other testing framework which is also compatible with `go test` Snapshots are auto-generated and managed by this package:

Index

Constants

View Source
const (
	// SnapshotDir is where snapshots are stored
	SnapshotDir string = "__snapshots__"
	// SnapshotExtension is the extension for snapshot files
	SnapshotExtension string = ".snap.xml"
	// Indent - 4 spaces
	Indent string = "    "
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparison

type Comparison interface {
	// Matches runs a comparison with the saved snapshot
	// overwrites the saved snapshot if CLI flag "-u" is passed to `go test`
	Matches(v interface{}) error
}

Comparison compares snapshots

func WithLabel

func WithLabel(label string) Comparison

WithLabel reads the existing snapshot and allows comparison

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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