Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve TestMain references #86

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ go help testflag

`go-snaps` can identify obsolete snapshots.

In order to enable this functionality you need to use `TestMain(t*testing.M)` to
In order to enable this functionality you need to use `TestMain(m *testing.M)` to
call `snaps.Clean(t)` after your tests have run. This will also print a **Snapshot Summary**. (if running tests
with verbose flag `-v`)

Expand All @@ -258,11 +258,11 @@ are finished so it can keep track of which snapshots were not called.
**Example:**

```go
func TestMain(t *testing.M) {
v := t.Run()
func TestMain(m *testing.M) {
v := m.Run()

// After all tests have run `go-snaps` can check for not used snapshots
snaps.Clean(t)
// After all tests have run `go-snaps` can check for unused snapshots
snaps.Clean(m)

os.Exit(v)
}
Expand All @@ -275,11 +275,11 @@ For more information around [TestMain](https://pkg.go.dev/testing#hdr-Main).
By default `go-snaps` appends new snaps to the snapshot file and in case of parallel tests the order is random. If you want snaps to be sorted in deterministic order you need to use `TestMain` per package:

```go
func TestMain(t *testing.M) {
v := t.Run()
func TestMain(m *testing.M) {
v := m.Run()

// After all tests have run `go-snaps` will sort snapshots
snaps.Clean(t, snaps.CleanOpts{Sort: true})
snaps.Clean(m, snaps.CleanOpts{Sort: true})

os.Exit(v)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/gkampitakis/go-snaps/snaps"
)

func TestMain(t *testing.M) {
v := t.Run()
func TestMain(m *testing.M) {
v := m.Run()

snaps.Clean(t)
snaps.Clean(m)

os.Exit(v)
}
Expand Down
14 changes: 7 additions & 7 deletions snaps/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ type CleanOpts struct {
//
// Must be called in a TestMain
//
// func TestMain(t *testing.M) {
// v := t.Run()
// func TestMain(m *testing.M) {
// v := m.Run()
//
// // After all tests have run `go-snaps` can check for not used snapshots
// snaps.Clean(t)
// // After all tests have run `go-snaps` can check for unused snapshots
// snaps.Clean(m)
//
// os.Exit(v)
// }
//
// Clean also supports options for sorting the snapshots
//
// func TestMain(t *testing.M) {
// v := t.Run()
// func TestMain(m *testing.M) {
// v := m.Run()
//
// // After all tests have run `go-snaps` will sort snapshots
// snaps.Clean(t, snaps.CleanOpts{Sort: true})
// snaps.Clean(m, snaps.CleanOpts{Sort: true})
//
// os.Exit(v)
// }
Expand Down