From 864d5babdcb082625758709b083c5c8b5a6994f4 Mon Sep 17 00:00:00 2001 From: Georgios Kampitakis <50364739+gkampitakis@users.noreply.github.com> Date: Sun, 30 Oct 2022 13:27:34 +0000 Subject: [PATCH] feat: implement MatchJSON snapshot function (#49) --- README.md | 74 +++++--- go.mod | 6 +- go.sum | 6 + golangci.yml | 2 +- {snaps/internal => internal}/colors/colors.go | 0 .../colors/colors_test.go | 2 +- .../internal => internal}/difflib/difflib.go | 0 .../difflib/difflib_test.go | 2 +- {snaps/internal => internal}/test/test.go | 0 snaps/clean.go | 2 +- snaps/clean_test.go | 2 +- snaps/diff.go | 4 +- snaps/diff_test.go | 4 +- snaps/matchJSON.go | 110 +++++++++++ snaps/matchJSON_test.go | 177 ++++++++++++++++++ snaps/matchSnapshot.go | 3 +- snaps/matchSnapshot_test.go | 118 ++++-------- snaps/skip.go | 2 +- snaps/skip_test.go | 2 +- snaps/snapshot.go | 2 +- snaps/snapshot_test.go | 2 +- snaps/utils_test.go | 2 +- 22 files changed, 401 insertions(+), 121 deletions(-) rename {snaps/internal => internal}/colors/colors.go (100%) rename {snaps/internal => internal}/colors/colors_test.go (98%) rename {snaps/internal => internal}/difflib/difflib.go (100%) rename {snaps/internal => internal}/difflib/difflib_test.go (98%) rename {snaps/internal => internal}/test/test.go (100%) create mode 100644 snaps/matchJSON.go create mode 100644 snaps/matchJSON_test.go diff --git a/README.md b/README.md index e611482..cb665ab 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,12 @@ ## Highlights - [Installation](#installation) -- [Usage](#usage) +- [MatchSnapshot](#matchsnapshot) +- [MatchJSON](#matchjson) - [Update Snapshots](#update-snapshots) + - [Clean obsolete Snapshots](#clean-obsolete-snapshots) + - [Skipping Tests](#skipping-tests) - [No Color](#no-color) -- [Clean obsolete Snapshots](#clean-obsolete-snapshots) - [Snapshots Structure](#snapshots-structure) - [Acknowledgments](#acknowledgments) - [Contributing](./contributing.md) @@ -60,7 +62,9 @@ func TestExample(t *testing.T) { } ``` -## Usage +## MatchSnapshot + +`MatchSnapshot` can be used to capture any type of data structured or unstructured. You can pass multiple parameters to `MatchSnapshot` or call `MatchSnapshot` multiple times inside the same test. The difference is in the latter, it will @@ -83,6 +87,29 @@ name is the test file name with extension `.snap`. So for example if your test is called `test_simple.go` when you run your tests, a snapshot file will be created at `./__snapshots__/test_simple.snaps`. + +## MatchJSON + +`MatchJSON` can be used to capture data that can represent a valid json. + +You can pass a valid json in form of `string` or `[]byte` or whatever value can be passed +successfully on `json.Marshal`. + +```go +func TestJSON(t *testing.T) { + type User struct { + Age int + Email string + } + + snaps.MatchJSON(t, `{"user":"mock-user","age":10,"email":"mock@email.com"}`) + snaps.MatchJSON(t, []byte(`{"user":"mock-user","age":10,"email":"mock@email.com"}`)) + snaps.MatchJSON(t, User{10, "mock-email"}) +} +``` + +JSON will be saved in snapshot in pretty format for more readability. + ## Update Snapshots You can update your failing snapshots by setting `UPDATE_SNAPS` env variable to true. @@ -99,18 +126,7 @@ For more information for `go test` flags you can run go help testflag ``` -## No Color - -`go-snaps` supports disabling color outputs by running your tests with the env variable -`NO_COLOR` set to any value. - -```bash -NO_COLOR=true go test ./... -``` - -For more information around [NO_COLOR](https://no-color.org). - -## Clean obsolete snapshots +### Clean obsolete snapshots

Summary Obsolete @@ -144,17 +160,27 @@ func TestMain(t *testing.M) { For more information around [TestMain](https://pkg.go.dev/testing#hdr-Main). -#### Skipping Tests +### Skipping Tests If you want to skip one test using `t.Skip`, `go-snaps` can't keep track if the test was skipped or if it was removed. For that reason `go-snaps` exposes -a light wrapper for `t.Skip`, `t.Skipf` and `t.SkipNow` which help `go-snaps` identify -the skipped tests. +a wrapper for `t.Skip`, `t.Skipf` and `t.SkipNow`, which keep tracks of skipped files. You can skip, or only run specific tests by using the `-run` flag. `go-snaps` -can "understand" which tests are being skipped and parse only the relevant tests +can identify which tests are being skipped and parse only the relevant tests for obsolete snapshots. +## No Color + +`go-snaps` supports disabling color outputs by running your tests with the env variable +`NO_COLOR` set to any value. + +```bash +NO_COLOR=true go test ./... +``` + +For more information around [NO_COLOR](https://no-color.org). + ## Snapshots Structure Snapshots have the form @@ -179,6 +205,8 @@ map[string]interface {}{ --- ``` +> `*.snap` files are not meant to be edited manually, this might cause unexpected results. + ## Acknowledgments This library used [Jest Snapshoting](https://jestjs.io/docs/snapshot-testing) and [Cupaloy](https://github.com/bradleyjkemp/cupaloy) as inspiration. @@ -198,10 +226,4 @@ This library used [Jest Snapshoting](https://jestjs.io/docs/snapshot-testing) an and after a new line, `go-snaps` will "escape" them and save them as `/-/-/-/`. This should not cause any diff issues (false-positives). -4. Snapshots should be treated as code. - -5. `.snap` files are not meant to be edited manually, this might cause unexpected results. - -#### License - -MIT +4. Snapshots should be treated as code. The snapshot artifact should be committed alongside code changes, and reviewed as part of your code review process diff --git a/go.mod b/go.mod index b670c0a..f8c093e 100644 --- a/go.mod +++ b/go.mod @@ -8,4 +8,8 @@ require ( github.com/kr/pretty v0.3.0 ) -require github.com/rogpeppe/go-internal v1.9.0 // indirect +require ( + github.com/rogpeppe/go-internal v1.9.0 // indirect + github.com/tidwall/gjson v1.14.3 + github.com/tidwall/pretty v1.2.0 +) diff --git a/go.sum b/go.sum index bdb851e..1abba0c 100644 --- a/go.sum +++ b/go.sum @@ -14,5 +14,11 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= +github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= diff --git a/golangci.yml b/golangci.yml index 0a237f8..e588c64 100644 --- a/golangci.yml +++ b/golangci.yml @@ -24,4 +24,4 @@ linters-settings: run: skip-files: - - snaps/internal/difflib/difflib.go + - internal/difflib/difflib.go diff --git a/snaps/internal/colors/colors.go b/internal/colors/colors.go similarity index 100% rename from snaps/internal/colors/colors.go rename to internal/colors/colors.go diff --git a/snaps/internal/colors/colors_test.go b/internal/colors/colors_test.go similarity index 98% rename from snaps/internal/colors/colors_test.go rename to internal/colors/colors_test.go index 9579a22..58c0f91 100644 --- a/snaps/internal/colors/colors_test.go +++ b/internal/colors/colors_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "github.com/gkampitakis/go-snaps/snaps/internal/test" + "github.com/gkampitakis/go-snaps/internal/test" ) func TestPrintColors(t *testing.T) { diff --git a/snaps/internal/difflib/difflib.go b/internal/difflib/difflib.go similarity index 100% rename from snaps/internal/difflib/difflib.go rename to internal/difflib/difflib.go diff --git a/snaps/internal/difflib/difflib_test.go b/internal/difflib/difflib_test.go similarity index 98% rename from snaps/internal/difflib/difflib_test.go rename to internal/difflib/difflib_test.go index a325677..3340509 100644 --- a/snaps/internal/difflib/difflib_test.go +++ b/internal/difflib/difflib_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "github.com/gkampitakis/go-snaps/snaps/internal/test" + "github.com/gkampitakis/go-snaps/internal/test" ) func TestGetOptCodes(t *testing.T) { diff --git a/snaps/internal/test/test.go b/internal/test/test.go similarity index 100% rename from snaps/internal/test/test.go rename to internal/test/test.go diff --git a/snaps/clean.go b/snaps/clean.go index 95f52e0..8965474 100644 --- a/snaps/clean.go +++ b/snaps/clean.go @@ -12,7 +12,7 @@ import ( "sync" "testing" - "github.com/gkampitakis/go-snaps/snaps/internal/colors" + "github.com/gkampitakis/go-snaps/internal/colors" ) // Matches [ Test... - number ] testIDs diff --git a/snaps/clean_test.go b/snaps/clean_test.go index bf513ac..4a84262 100644 --- a/snaps/clean_test.go +++ b/snaps/clean_test.go @@ -7,7 +7,7 @@ import ( "sort" "testing" - "github.com/gkampitakis/go-snaps/snaps/internal/test" + "github.com/gkampitakis/go-snaps/internal/test" ) const mockSnap1 = ` diff --git a/snaps/diff.go b/snaps/diff.go index e63dc16..4f3b9db 100644 --- a/snaps/diff.go +++ b/snaps/diff.go @@ -7,8 +7,8 @@ import ( "strings" "github.com/gkampitakis/go-diff/diffmatchpatch" - "github.com/gkampitakis/go-snaps/snaps/internal/colors" - "github.com/gkampitakis/go-snaps/snaps/internal/difflib" + "github.com/gkampitakis/go-snaps/internal/colors" + "github.com/gkampitakis/go-snaps/internal/difflib" ) const ( diff --git a/snaps/diff_test.go b/snaps/diff_test.go index a092781..514942d 100644 --- a/snaps/diff_test.go +++ b/snaps/diff_test.go @@ -4,8 +4,8 @@ import ( "strings" "testing" - "github.com/gkampitakis/go-snaps/snaps/internal/colors" - "github.com/gkampitakis/go-snaps/snaps/internal/test" + "github.com/gkampitakis/go-snaps/internal/colors" + "github.com/gkampitakis/go-snaps/internal/test" ) var a = `Proin justo libero, pellentesque sit amet scelerisque ut, sollicitudin non tortor. diff --git a/snaps/matchJSON.go b/snaps/matchJSON.go new file mode 100644 index 0000000..54fa6ed --- /dev/null +++ b/snaps/matchJSON.go @@ -0,0 +1,110 @@ +package snaps + +import ( + "encoding/json" + "errors" + + "github.com/tidwall/gjson" + "github.com/tidwall/pretty" +) + +var ( + jsonOptions = &pretty.Options{ + SortKeys: true, + Indent: " ", + } + errInvalidJSON = errors.New("invalid json") +) + +/* +MatchJSON verifies the input matches the most recent snap file. +Input can be a valid json string or []byte or whatever value can be passed +successfully on `json.Marshal`. + + MatchJSON(t, `{"user":"mock-user","age":10,"email":"mock@email.com"}`) + MatchJSON(t, []byte(`{"user":"mock-user","age":10,"email":"mock@email.com"}`)) + MatchJSON(t, User{10, "mock-email"}) + +matchers is placeholder for now. +*/ +func MatchJSON(t testingT, input interface{}, matchers ...interface{}) { + t.Helper() + + dir, snapPath := snapDirAndName() + testID := testsRegistry.getTestID(t.Name(), snapPath) + + j, err := validateJSON(input) + if err != nil { + handleError(t, err) + return + } + + snapshot := takeJSONSnapshot(j) + if err != nil { + handleError(t, err) + return + } + prevSnapshot, err := getPrevSnapshot(testID, snapPath) + if errors.Is(err, errSnapNotFound) { + if isCI { + handleError(t, err) + return + } + + err := addNewSnapshot(testID, snapshot, dir, snapPath) + if err != nil { + handleError(t, err) + return + } + + t.Log(addedMsg) + testEvents.register(added) + return + } + if err != nil { + handleError(t, err) + return + } + + diff := prettyDiff(prevSnapshot, snapshot) + if diff == "" { + testEvents.register(passed) + return + } + + if !shouldUpdate { + handleError(t, diff) + return + } + + if err = updateSnapshot(testID, snapshot, snapPath); err != nil { + handleError(t, err) + return + } + + t.Log(updatedMsg) + testEvents.register(updated) +} + +func validateJSON(input interface{}) ([]byte, error) { + switch j := input.(type) { + case string: + if !gjson.Valid(j) { + return nil, errInvalidJSON + } + + return []byte(j), nil + case []byte: + if !gjson.ValidBytes(j) { + return nil, errInvalidJSON + } + + return j, nil + default: + return json.Marshal(input) + } +} + +func takeJSONSnapshot(b []byte) string { + return string(pretty.PrettyOptions(b, jsonOptions)) +} diff --git a/snaps/matchJSON_test.go b/snaps/matchJSON_test.go new file mode 100644 index 0000000..f3ab16f --- /dev/null +++ b/snaps/matchJSON_test.go @@ -0,0 +1,177 @@ +package snaps + +import ( + "testing" + + "github.com/gkampitakis/go-snaps/internal/test" +) + +const jsonFilename = "matchJSON_test.snap" + +func TestMatchJSON(t *testing.T) { + t.Run("should create json snapshot", func(t *testing.T) { + expected := ` +{ + "items": [ + 5, + 1, + 3, + 4 + ], + "user": "mock-name" +} +` + + for _, tc := range []struct { + name string + input interface{} + }{ + { + name: "string", + input: `{"user":"mock-name","items":[5,1,3,4]}`, + }, + { + name: "string", + input: []byte(`{"user":"mock-name","items":[5,1,3,4]}`), + }, + { + name: "marshal object", + input: struct { + User string `json:"user"` + Items []int `json:"items"` + }{ + User: "mock-name", + Items: []int{5, 1, 3, 4}, + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + snapPath := setupSnapshot(t, jsonFilename, false) + + mockT := test.MockTestingT{ + MockHelper: func() {}, + MockName: func() string { + return "mock-name" + }, + MockError: func(args ...interface{}) { + test.NotCalled(t) + }, + MockLog: func(args ...interface{}) { test.Equal(t, addedMsg, args[0]) }, + } + + MatchJSON(mockT, tc.input) + + snap, err := getPrevSnapshot("mock-name - 1", snapPath) + test.Equal(t, nil, err) + test.Equal(t, expected, snap) + test.Equal(t, 1, testEvents.items[added]) + }) + } + }) + + t.Run("should validate json", func(t *testing.T) { + for _, tc := range []struct { + name string + input interface{} + err string + }{ + { + name: "string", + input: "", + err: "invalid json", + }, + { + name: "byte", + input: []byte(`{"user"`), + err: "invalid json", + }, + { + name: "struct", + input: make(chan struct{}), + err: "json: unsupported type: chan struct {}", + }, + } { + t.Run(tc.name, func(t *testing.T) { + setupSnapshot(t, jsonFilename, false) + + mockT := test.MockTestingT{ + MockHelper: func() {}, + MockName: func() string { + return "mock-name" + }, + MockError: func(args ...interface{}) { + test.Equal(t, tc.err, (args[0].(error)).Error()) + }, + MockLog: func(args ...interface{}) { + // this is called when snapshot is written successfully + test.NotCalled(t) + }, + } + + MatchJSON(mockT, tc.input) + }) + } + }) + + t.Run("if it's running on ci should skip creating snapshot", func(t *testing.T) { + snapPath := setupSnapshot(t, jsonFilename, true) + + mockT := test.MockTestingT{ + MockHelper: func() {}, + MockName: func() string { + return "mock-name" + }, + MockError: func(args ...interface{}) { + test.Equal(t, errSnapNotFound, args[0]) + }, + MockLog: func(args ...interface{}) { + test.NotCalled(t) + }, + } + + MatchJSON(mockT, "{}") + + _, err := snapshotFileToString(snapPath) + test.Equal(t, errSnapNotFound, err) + test.Equal(t, 1, testEvents.items[erred]) + }) + + t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) { + snapPath := setupSnapshot(t, jsonFilename, false, true) + + printerExpectedCalls := []func(received interface{}){ + func(received interface{}) { test.Equal(t, addedMsg, received) }, + func(received interface{}) { test.Equal(t, updatedMsg, received) }, + } + mockT := test.MockTestingT{ + MockHelper: func() {}, + MockName: func() string { + return "mock-name" + }, + MockError: func(args ...interface{}) { + test.NotCalled(t) + }, + MockLog: func(args ...interface{}) { + printerExpectedCalls[0](args[0]) + + // shift + printerExpectedCalls = printerExpectedCalls[1:] + }, + } + + // First call for creating the snapshot + MatchJSON(mockT, "{\"value\":\"hello world\"}") + test.Equal(t, 1, testEvents.items[added]) + + // Resetting registry to emulate the same MatchSnapshot call + testsRegistry = newRegistry() + + // Second call with different params + MatchJSON(mockT, "{\"value\":\"bye world\"}") + + snap, err := snapshotFileToString(snapPath) + test.Equal(t, nil, err) + test.Equal(t, "\n[mock-name - 1]\n{\n \"value\": \"bye world\"\n}\n---\n", snap) + test.Equal(t, 1, testEvents.items[updated]) + }) +} diff --git a/snaps/matchSnapshot.go b/snaps/matchSnapshot.go index f889250..d211f6c 100644 --- a/snaps/matchSnapshot.go +++ b/snaps/matchSnapshot.go @@ -3,7 +3,7 @@ package snaps import ( "errors" - "github.com/gkampitakis/go-snaps/snaps/internal/colors" + "github.com/gkampitakis/go-snaps/internal/colors" "github.com/kr/pretty" ) @@ -31,7 +31,6 @@ func MatchSnapshot(t testingT, values ...interface{}) { testID := testsRegistry.getTestID(t.Name(), snapPath) snapshot := takeSnapshot(values) prevSnapshot, err := getPrevSnapshot(testID, snapPath) - if errors.Is(err, errSnapNotFound) { if isCI { handleError(t, err) diff --git a/snaps/matchSnapshot_test.go b/snaps/matchSnapshot_test.go index db4d573..b7debcf 100644 --- a/snaps/matchSnapshot_test.go +++ b/snaps/matchSnapshot_test.go @@ -7,11 +7,13 @@ import ( "testing" "github.com/gkampitakis/ciinfo" - "github.com/gkampitakis/go-snaps/snaps/internal/colors" - "github.com/gkampitakis/go-snaps/snaps/internal/test" + "github.com/gkampitakis/go-snaps/internal/colors" + "github.com/gkampitakis/go-snaps/internal/test" ) -const mockSnap = ` +const ( + fileName = "matchSnapshot_test.snap" + mockSnap = ` [Test_1/TestSimple - 1] int(1) @@ -29,23 +31,37 @@ string hello world 1 3 2 --- ` +) -func TestMatchSnapshot(t *testing.T) { - t.Run("should create snapshot", func(t *testing.T) { - dir, _ := os.Getwd() - snapPath := filepath.Join(dir, "__snapshots__", "matchSnapshot_test.snap") - isCI = false +func setupSnapshot(t *testing.T, file string, ci bool, update ...bool) string { + t.Helper() + dir, _ := os.Getwd() + snapPath := filepath.Join(dir, "__snapshots__", file) + isCI = ci + shouldUpdatePrev := shouldUpdate + shouldUpdate = false + if len(update) > 0 { + shouldUpdate = update[0] + } + + t.Cleanup(func() { + os.Remove(snapPath) + testsRegistry = newRegistry() + testEvents = newTestEvents() + isCI = ciinfo.IsCI + shouldUpdate = shouldUpdatePrev + }) - t.Cleanup(func() { - os.Remove(snapPath) - testsRegistry = newRegistry() - testEvents = newTestEvents() - isCI = ciinfo.IsCI - }) + _, err := os.Stat(snapPath) + // This is for checking we are starting with a clean state testing + test.Equal(t, true, errors.Is(err, os.ErrNotExist)) - _, err := os.Stat(snapPath) + return snapPath +} - test.Equal(t, true, errors.Is(err, os.ErrNotExist)) +func TestMatchSnapshot(t *testing.T) { + t.Run("should create snapshot", func(t *testing.T) { + snapPath := setupSnapshot(t, fileName, false) mockT := test.MockTestingT{ MockHelper: func() {}, @@ -67,20 +83,7 @@ func TestMatchSnapshot(t *testing.T) { }) t.Run("if it's running on ci should skip", func(t *testing.T) { - dir, _ := os.Getwd() - snapPath := filepath.Join(dir, "__snapshots__", "matchSnapshot_test.snap") - isCI = true - - t.Cleanup(func() { - os.Remove(snapPath) - testsRegistry = newRegistry() - testEvents = newTestEvents() - isCI = ciinfo.IsCI - }) - - _, err := os.Stat(snapPath) - - test.Equal(t, true, errors.Is(err, os.ErrNotExist)) + snapPath := setupSnapshot(t, fileName, true) mockT := test.MockTestingT{ MockHelper: func() {}, @@ -97,31 +100,18 @@ func TestMatchSnapshot(t *testing.T) { MatchSnapshot(mockT, 10, "hello world") - _, err = snapshotFileToString(snapPath) + _, err := snapshotFileToString(snapPath) test.Equal(t, errSnapNotFound, err) test.Equal(t, 1, testEvents.items[erred]) }) t.Run("should return error when diff is found", func(t *testing.T) { - dir, _ := os.Getwd() - snapPath := filepath.Join(dir, "__snapshots__", "matchSnapshot_test.snap") + setupSnapshot(t, fileName, false) + printerExpectedCalls := []func(received interface{}){ func(received interface{}) { test.Equal(t, addedMsg, received) }, func(received interface{}) { test.NotCalled(t) }, } - isCI = false - - t.Cleanup(func() { - os.Remove(snapPath) - testsRegistry = newRegistry() - testEvents = newTestEvents() - isCI = ciinfo.IsCI - }) - - _, err := os.Stat(snapPath) - // This is for checking we are starting with a clean state testing - test.Equal(t, true, errors.Is(err, os.ErrNotExist)) - mockT := test.MockTestingT{ MockHelper: func() {}, MockName: func() string { @@ -156,28 +146,12 @@ func TestMatchSnapshot(t *testing.T) { }) t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) { - dir, _ := os.Getwd() - snapPath := filepath.Join(dir, "__snapshots__", "matchSnapshot_test.snap") - isCI = false - shouldUpdatePrev := shouldUpdate - shouldUpdate = true + snapPath := setupSnapshot(t, fileName, false, true) + printerExpectedCalls := []func(received interface{}){ func(received interface{}) { test.Equal(t, addedMsg, received) }, func(received interface{}) { test.Equal(t, updatedMsg, received) }, } - - t.Cleanup(func() { - os.Remove(snapPath) - testsRegistry = newRegistry() - testEvents = newTestEvents() - isCI = ciinfo.IsCI - shouldUpdate = shouldUpdatePrev - }) - - _, err := os.Stat(snapPath) - // This is for checking we are starting with a clean state testing - test.Equal(t, true, errors.Is(err, os.ErrNotExist)) - mockT := test.MockTestingT{ MockHelper: func() {}, MockName: func() string { @@ -226,24 +200,12 @@ func TestMatchSnapshot(t *testing.T) { }) t.Run("diff should not print the escaped characters", func(t *testing.T) { - dir, _ := os.Getwd() - snapPath := filepath.Join(dir, "__snapshots__", "matchSnapshot_test.snap") + setupSnapshot(t, fileName, false) + printerExpectedCalls := []func(received interface{}){ func(received interface{}) { test.Equal(t, addedMsg, received) }, func(received interface{}) { test.NotCalled(t) }, } - isCI = false - - t.Cleanup(func() { - os.Remove(snapPath) - testsRegistry = newRegistry() - isCI = ciinfo.IsCI - }) - - _, err := os.Stat(snapPath) - // This is for checking we are starting with a clean state testing - test.Equal(t, true, errors.Is(err, os.ErrNotExist)) - mockT := test.MockTestingT{ MockHelper: func() {}, MockName: func() string { diff --git a/snaps/skip.go b/snaps/skip.go index 0f5edaf..56c22dd 100644 --- a/snaps/skip.go +++ b/snaps/skip.go @@ -8,7 +8,7 @@ import ( "regexp" "strings" - "github.com/gkampitakis/go-snaps/snaps/internal/colors" + "github.com/gkampitakis/go-snaps/internal/colors" ) var ( diff --git a/snaps/skip_test.go b/snaps/skip_test.go index dc2d866..ddad462 100644 --- a/snaps/skip_test.go +++ b/snaps/skip_test.go @@ -5,7 +5,7 @@ import ( "sync" "testing" - "github.com/gkampitakis/go-snaps/snaps/internal/test" + "github.com/gkampitakis/go-snaps/internal/test" ) func TestSkip(t *testing.T) { diff --git a/snaps/snapshot.go b/snaps/snapshot.go index 4379dc5..1288616 100644 --- a/snaps/snapshot.go +++ b/snaps/snapshot.go @@ -8,7 +8,7 @@ import ( "strings" "sync" - "github.com/gkampitakis/go-snaps/snaps/internal/colors" + "github.com/gkampitakis/go-snaps/internal/colors" ) var ( diff --git a/snaps/snapshot_test.go b/snaps/snapshot_test.go index 412a9f4..2419818 100644 --- a/snaps/snapshot_test.go +++ b/snaps/snapshot_test.go @@ -6,7 +6,7 @@ import ( "sync" "testing" - "github.com/gkampitakis/go-snaps/snaps/internal/test" + "github.com/gkampitakis/go-snaps/internal/test" ) func TestTestID(t *testing.T) { diff --git a/snaps/utils_test.go b/snaps/utils_test.go index a9719ae..abbfdb9 100644 --- a/snaps/utils_test.go +++ b/snaps/utils_test.go @@ -3,7 +3,7 @@ package snaps import ( "testing" - "github.com/gkampitakis/go-snaps/snaps/internal/test" + "github.com/gkampitakis/go-snaps/internal/test" ) func TestBaseCallerNested(t *testing.T) {