-
Notifications
You must be signed in to change notification settings - Fork 2
/
golden_approval_test.go
115 lines (90 loc) · 3.48 KB
/
golden_approval_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
package golden_test
import (
"github.com/franiglesias/golden"
"github.com/franiglesias/golden/internal/helper"
"github.com/franiglesias/golden/internal/vfs"
"testing"
)
/*
TestApproval needs the same setup as TestVerify. Check it for documentation.
*/
func TestApproval(t *testing.T) {
var gld golden.Golden
var fs *vfs.MemFs
var tSpy helper.TSpy
setUp := func(t *testing.T) {
fs = vfs.NewMemFs()
gld = *golden.NewUsingFs(fs)
tSpy = helper.TSpy{
T: t,
}
}
t.Run("should create snapshot and fail", func(t *testing.T) {
setUp(t)
gld.Verify(&tSpy, "some subject.", golden.WaitApproval())
vfs.AssertSnapshotWasCreated(t, fs, "testdata/TestApproval/should_create_snapshot_and_fail.snap")
helper.AssertFailedTest(t, &tSpy)
})
/*
Simulates the process of running approval tests so snapshot is never taken as
criteria for matching, meaning that you are waiting for human approval before
changing test type to Verify
*/
t.Run("should keep test failing while approval mode", func(t *testing.T) {
setUp(t)
gld.Verify(&tSpy, "starting subject.", golden.WaitApproval())
helper.AssertFailedTest(t, &tSpy)
vfs.AssertContentWasStored(t, fs, "testdata/TestApproval/should_keep_test_failing_while_approval_mode.snap", []byte("starting subject."))
tSpy.Reset()
gld.Verify(&tSpy, "updated subject.", golden.WaitApproval())
helper.AssertFailedTest(t, &tSpy)
vfs.AssertContentWasStored(t, fs, "testdata/TestApproval/should_keep_test_failing_while_approval_mode.snap", []byte("updated subject."))
tSpy.Reset()
})
/*
Simulates the process of running approval tests until you obtain approval for
the generated snapshot
*/
t.Run("should accept snapshot at Verify", func(t *testing.T) {
setUp(t)
gld.Verify(&tSpy, "starting subject.", golden.WaitApproval())
tSpy.Reset()
// After this run the snapshot will be approved by an expert
gld.Verify(&tSpy, "updated subject.", golden.WaitApproval())
tSpy.Reset()
// At this point, the snapshot was approved, so we can change the test back to
// Verification mode, removing the golden.WaitApproval() option
gld.Verify(&tSpy, "updated subject.")
helper.AssertPassTest(t, &tSpy)
})
/*
Simulates the process of running approval tests until you obtain approval for
the generated snapshot, but with custom snapshot file name
*/
t.Run("should work with custom snapshot", func(t *testing.T) {
setUp(t)
gld.Verify(&tSpy, "starting subject.", golden.Snapshot("approval_snapshot"), golden.WaitApproval())
tSpy.Reset()
gld.Verify(&tSpy, "updated subject.", golden.Snapshot("approval_snapshot"), golden.WaitApproval())
tSpy.Reset()
gld.Verify(&tSpy, "updated subject.", golden.Snapshot("approval_snapshot"))
helper.AssertPassTest(t, &tSpy)
})
t.Run("should detect and report differences first run", func(t *testing.T) {
setUp(t)
// Sets the snapshot for first time
gld.Verify(&tSpy, "original output.", golden.WaitApproval())
// Report should show original content as differences
helper.AssertFailedTest(t, &tSpy)
helper.AssertReportContains(t, &tSpy, "+original output.\n")
})
t.Run("should detect and report differences subsequent run", func(t *testing.T) {
setUp(t)
// Sets the snapshot for first time
gld.Verify(&tSpy, "original output.", golden.WaitApproval())
// Changes happened. Verify against existing snapshot
gld.Verify(&tSpy, "different output.", golden.WaitApproval())
helper.AssertFailedTest(t, &tSpy)
helper.AssertReportContains(t, &tSpy, "-original output.\n+different output.\n")
})
}