https://github.com/natescarlet/snapshot
Do snapshot test in go
https://github.com/natescarlet/snapshot
snapshot testing
Last synced: 5 months ago
JSON representation
Do snapshot test in go
- Host: GitHub
- URL: https://github.com/natescarlet/snapshot
- Owner: NateScarlet
- License: mit
- Created: 2020-11-25T18:36:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-11-23T13:45:14.000Z (over 1 year ago)
- Last Synced: 2025-11-22T14:58:46.849Z (8 months ago)
- Topics: snapshot, testing
- Language: Go
- Homepage:
- Size: 66.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Snapshot
[](https://pkg.go.dev/github.com/NateScarlet/snapshot/pkg/snapshot)
[](https://github.com/NateScarlet/snapshot/actions)
Snapshot test for golang.
Store snapshot files under `__snapshots__` folder relative to caller file.
- [x] json snapshot for any go object with type info
- [x] transform value as schema to compare type only
- [x] clean data by regexp before compare
## Update snapshot
set `SNAPSHOT_UPDATE` env var to `true` to update existed snapshot file.
## Usage
```go
import (
"time"
"github.com/NateScarlet/snapshot/pkg/snapshot"
)
type Object struct {
A string
B int
C bool
D time.Time
}
func TestSomeThing(t *testing.T) {
// Match as text
snapshot.Match(t, "text")
// snapshot:
// text
// Match as json
snapshot.MatchJSON(t, Object{})
// snapshot:
// {
// "$Object": {
// "A": "",
// "B": 0,
// "C": false,
// "D": {
// "$Time": "0001-01-01 00:00:00 +0000 UTC"
// }
// }
// }
// Match schema as json
snapshot.MatchJSON(t, Object{}, snapshot.OptionTransform(snapshot.TransformSchema))
// snapshot:
// {
// "$Object": {
// "A": "$string",
// "B": "$int",
// "C": "$bool",
// "D": "$Time"
// }
// }
// Clean dynamic data to make result deterministic
snapshot.MatchJSON(t,
Object{
A: "a",
B: 1,
C: true,
D: time.Now(),
},
snapshot.OptionCleanRegex(
snapshot.CleanAs("*time*"),
`"D": {\s+"\$Time": "(.+)"\s+}`,
),
)
// snapshot:
// {
// "$Object": {
// "A": "a",
// "B": 1,
// "C": true,
// "D": {
// "$Time": "*time*"
// }
// }
// }
}
```