/
matchSnapshot_test.go
124 lines (104 loc) 路 2.64 KB
/
matchSnapshot_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
package examples
import (
"bytes"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/gkampitakis/go-snaps/snaps"
)
func TestMain(m *testing.M) {
v := m.Run()
snaps.Clean(m)
os.Exit(v)
}
func TestMatchSnapshot(t *testing.T) {
t.Run("should make an int snapshot", func(t *testing.T) {
snaps.MatchSnapshot(t, 5)
})
t.Run("should make a string snapshot", func(t *testing.T) {
snaps.MatchSnapshot(t, "string snapshot")
})
t.Run("should make a map snapshot", func(t *testing.T) {
m := map[string]any{
"mock-0": "value",
"mock-1": 2,
"mock-2": func() {},
"mock-3": float32(10.4),
}
snaps.MatchSnapshot(t, m)
})
t.Run("should make multiple entries in snapshot", func(t *testing.T) {
snaps.MatchSnapshot(t, 5, 10, 20, 25)
})
t.Run("should make create multiple snapshot", func(t *testing.T) {
snaps.MatchSnapshot(t, 1000)
snaps.MatchSnapshot(t, "another snapshot")
snaps.MatchSnapshot(t, `{
"user": "gkampitakis",
"id": 1234567,
"data": [ ]
}`)
})
t.Run("nest", func(t *testing.T) {
t.Run("more", func(t *testing.T) {
t.Run("one more nested test", func(t *testing.T) {
snaps.MatchSnapshot(t, "it's okay")
})
})
})
t.Run(".*", func(t *testing.T) {
snaps.MatchSnapshot(t, "ignore regex patterns on names")
})
t.Run("withConfig", func(t *testing.T) {
t.Run("should allow changing filename", func(t *testing.T) {
snaps.WithConfig(
snaps.Filename("custom_file"),
).MatchSnapshot(t, "snapshot data")
})
t.Run("should allow changing dir", func(t *testing.T) {
s := snaps.WithConfig(snaps.Dir("testdata"))
s.MatchSnapshot(t, "snapshot with different dir name")
s.MatchSnapshot(t, "another one", 1, 10)
})
t.Run("should allow absolute path", func(t *testing.T) {
_, b, _, _ := runtime.Caller(0)
basepath := filepath.Dir(b)
snaps.WithConfig(snaps.Dir(basepath+"/absolute_path")).
MatchSnapshot(t, "supporting absolute path")
})
s := snaps.WithConfig(snaps.Dir("special_data"), snaps.Filename("different_name"))
s.MatchSnapshot(t, "different data than the rest")
snaps.MatchSnapshot(t, "this should use the default config")
})
}
func TestSimpleTable(t *testing.T) {
type testCases struct {
description string
input any
}
for _, scenario := range []testCases{
{
description: "string",
input: "input",
},
{
description: "integer",
input: 10,
},
{
description: "map",
input: map[string]any{
"test": func() {},
},
},
{
description: "buffer",
input: bytes.NewBufferString("Buffer string"),
},
} {
t.Run(scenario.description, func(t *testing.T) {
snaps.MatchSnapshot(t, scenario.input)
})
}
}