Skip to content

Commit

Permalink
docs: improve TestMain references (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Jan 9, 2024
1 parent 9677416 commit a66844e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
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

0 comments on commit a66844e

Please sign in to comment.