-
Notifications
You must be signed in to change notification settings - Fork 2
/
global_verify_test.go
125 lines (90 loc) · 3.95 KB
/
global_verify_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package golden_test
import (
"fmt"
"github.com/franiglesias/golden"
"github.com/franiglesias/golden/internal/helper"
"github.com/franiglesias/golden/internal/vfs"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestGlobalVerify(t *testing.T) {
// fs is kind of an in-memory filesystem. This allows us to test the library
// without polluting the local file system with temporary files. This also allows
// us to inspect the generated paths and files
var fs *vfs.MemFs
// tSpy holds a replacement of the standard testing.T. This allows us to separate
// the simulated test from the test itself.
var tSpy helper.TSpy
setUp := func(t *testing.T) {
// Passing t in each setup guarantees that we are using the right name for the
// snapshot, otherwise the name won't be accurate
// Inits a new instance of Golden using an in-memory filesystem that will be
// empty on each test run
fs = vfs.NewMemFs()
golden.G = golden.NewUsingFs(fs)
// Replace testing.T with this double to allow spying results
tSpy = helper.TSpy{
T: t,
}
}
t.Run("should create snapshot if not exists", func(t *testing.T) {
setUp(t)
golden.Verify(t, "some subject.")
vfs.AssertSnapshotWasCreated(t, fs, "testdata/TestGlobalVerify/should_create_snapshot_if_not_exists.snap")
})
t.Run("should write subject as snapshot content", func(t *testing.T) {
setUp(t)
golden.Verify(t, "some output.")
expected := []byte("some output.")
vfs.AssertContentWasStored(t, fs, "testdata/TestGlobalVerify/should_write_subject_as_snapshot_content.snap", expected)
})
t.Run("should not alter snapshot when it already exists", func(t *testing.T) {
setUp(t)
golden.Verify(&tSpy, "some output.")
golden.Verify(&tSpy, "different output.")
want := []byte(("some output."))
vfs.AssertContentWasStored(t, fs, "testdata/TestGlobalVerify/should_not_alter_snapshot_when_it_already_exists.snap", want)
})
t.Run("should detect and report differences by line", func(t *testing.T) {
setUp(t)
// Sets the snapshot for first time
golden.Verify(&tSpy, "original output.")
// Changes happened. Verify against existing snapshot
golden.Verify(&tSpy, "different output.")
helper.AssertFailedTest(t, &tSpy)
helper.AssertReportContains(t, &tSpy, "-original output.\n+different output.\n")
})
t.Run("should use custom name for snapshot", func(t *testing.T) {
setUp(t)
golden.Verify(&tSpy, "original output", golden.Snapshot("custom_snapshot"))
vfs.AssertSnapshotWasCreated(t, fs, "testdata/custom_snapshot.snap")
})
t.Run("should use default name after spend customized", func(t *testing.T) {
setUp(t)
golden.Verify(&tSpy, "original output", golden.Snapshot("custom_snapshot"))
golden.Verify(&tSpy, "original output")
vfs.AssertSnapshotWasCreated(t, fs, "testdata/custom_snapshot.snap")
vfs.AssertSnapshotWasCreated(t, fs, "testdata/TestGlobalVerify/should_use_default_name_after_spend_customized.snap")
})
t.Run("should allow external file via custom name", func(t *testing.T) {
setUp(t)
// Creates a file in the path, simulating that we put our own
err := fs.WriteFile("testdata/external_snapshot.snap", []byte("external output"))
assert.NoError(t, err)
// By default, golden would create a snapshot. But given that we have a file in
// the expected path, Golden will use it as criteria, so test should fail given
// that subject and snapshot doesn't match
golden.Verify(&tSpy, "generated output", golden.Snapshot("external_snapshot"))
helper.AssertFailedTest(t, &tSpy)
})
t.Run("should scrub data", func(t *testing.T) {
setUp(t)
scrubber := golden.NewScrubber("\\d{2}:\\d{2}:\\d{2}.\\d{3}", "<Current Time>")
// Here we have a non-deterministic subject
subject := fmt.Sprintf("Current time is: %s", time.Now().Format("15:04:05.000"))
golden.Verify(&tSpy, subject, golden.WithScrubbers(scrubber))
helper.AssertPassTest(t, &tSpy)
vfs.AssertSnapShotContains(t, fs, "testdata/TestGlobalVerify/should_scrub_data.snap", "<Current Time>")
})
}