You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This will store the above slice in testdata/snapshots/TestSnapShot.snap.txt
74
+
// This will store the above slice in testdata/snapshots/TestSnapshot.snap.txt
74
75
// then all future checks will compare against this snapshot
75
76
}
76
77
```
@@ -86,6 +87,7 @@ Not very helpful if you want your snapshots to be as readable as possible!
86
87
With `snapshot`, you have full control over how your type is serialised to the snapshot file (if you need it). You can either:
87
88
88
89
- Let `snapshot` take a best guess at how to serialise your type
90
+
- With or without "filters" to tweak the result, see [Filters](#filters) below for more info
89
91
- Implement one of `snapshot.Snapper`, [json.Marshaler], [encoding.TextMarshaler], or [fmt.Stringer] to override how it's serialised
90
92
91
93
See [Serialisation Rules](#serialisation-rules) 👇🏻 for more info on how `snapshot` decides how to snap your value
@@ -181,6 +183,36 @@ Because of this, it needs to know how to serialise your value (which could be ba
181
183
> [!TIP]
182
184
> `snapshot` effectively goes through this list top to bottom to discover how to serialise your type, so mechanisms at the top are preferentially chosen over mechanisms lower down. If your snapshot doesn't look quite right, consider implementing a method higher up the list to get the behaviour you need
183
185
186
+
## Filters
187
+
188
+
Sometimes, your snapshots might contain data that is randomly generated like UUIDs, or constantly changing like timestamps, or that might change on different platforms like filepaths, temp directory names etc.
189
+
190
+
These things make snapshot testing annoying because your snapshot changes every time or passes locally but fails in CI on a windows GitHub Actions runner (my personal bane!). `snapshot` lets you add "filters" to your configuration to solve this.
191
+
192
+
Filters are simply regex replacements that you specify ahead of time, and if any of them appear in your snapshot, they can be replaced by whatever you want!
193
+
194
+
For example:
195
+
196
+
```go
197
+
funcTestWithFilters(t *testing.T) {
198
+
// Some common ones to give you inspiration
199
+
snap:= snapshot.New(
200
+
t,
201
+
snapshot.Filter("(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", "[UUID]"), // Replace uuids with the literal string "[UUID]"
202
+
snapshot.Filter(`\\([\w\d]|\.)`, "/$1"), // Replace windows file paths with unix equivalents
203
+
snapshot.Filter(`/var/folders/\S+?/T/\S+`, "[TEMP_DIR]"), // Replace a macos temp dir with the literal string "[TEMP_DIR]"
204
+
)
205
+
}
206
+
```
207
+
208
+
But you can imagine more:
209
+
210
+
- Unix timestamps
211
+
- Go `time.Duration` values
212
+
- Other types of uuids, ulids etc.
213
+
214
+
If you can find a regex for it, you can filter it out!
215
+
184
216
### Credits
185
217
186
218
This package was created with [copier] and the [FollowTheProcess/go_copier] project template.
0 commit comments