-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement MatchJSON snapshot function (#49)
- Loading branch information
1 parent
16a6ae1
commit 864d5ba
Showing
22 changed files
with
401 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ linters-settings: | |
|
||
run: | ||
skip-files: | ||
- snaps/internal/difflib/difflib.go | ||
- internal/difflib/difflib.go |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
Oops, something went wrong.