Skip to content

Commit c9b4196

Browse files
Update docs to include filter
1 parent 68d076d commit c9b4196

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
# Snapshot
2+
13
<p align="center">
24
<img src="https://github.com/FollowTheProcess/snapshot/raw/main/img/logo.jpg" alt="logo" width=75%>
35
</p>
46

5-
# Snapshot
6-
77
[![License](https://img.shields.io/github/license/FollowTheProcess/snapshot)](https://github.com/FollowTheProcess/snapshot)
88
[![Go Reference](https://pkg.go.dev/badge/github.com/FollowTheProcess/snapshot.svg)](https://pkg.go.dev/github.com/FollowTheProcess/snapshot)
99
[![Go Report Card](https://goreportcard.com/badge/github.com/FollowTheProcess/snapshot)](https://goreportcard.com/report/github.com/FollowTheProcess/snapshot)
@@ -26,6 +26,7 @@ Simple, intuitive snapshot testing with Go 📸
2626
- [🗑️ Tidying Up](#️-tidying-up)
2727
- [🤓 Follows Go Conventions](#-follows-go-conventions)
2828
- [Serialisation Rules](#serialisation-rules)
29+
- [Filters](#filters)
2930
- [Credits](#credits)
3031

3132
## Project Description
@@ -70,7 +71,7 @@ func TestSnapshot(t *testing.T) {
7071

7172
snap.Snap([]string{"hello", "there", "this", "is", "a", "snapshot"})
7273

73-
// 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
7475
// then all future checks will compare against this snapshot
7576
}
7677
```
@@ -86,6 +87,7 @@ Not very helpful if you want your snapshots to be as readable as possible!
8687
With `snapshot`, you have full control over how your type is serialised to the snapshot file (if you need it). You can either:
8788

8889
- 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
8991
- Implement one of `snapshot.Snapper`, [json.Marshaler], [encoding.TextMarshaler], or [fmt.Stringer] to override how it's serialised
9092

9193
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
181183
> [!TIP]
182184
> `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
183185
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+
func TestWithFilters(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+
184216
### Credits
185217

186218
This package was created with [copier] and the [FollowTheProcess/go_copier] project template.

0 commit comments

Comments
 (0)