Skip to content

Commit

Permalink
feat: implement matchStandaloneJSON (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis authored Dec 26, 2024
1 parent f81115d commit 560fde0
Show file tree
Hide file tree
Showing 23 changed files with 735 additions and 233 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [MatchSnapshot](#matchsnapshot)
- [MatchStandaloneSnapshot](#matchstandalonesnapshot) `New`
- [MatchJSON](#matchjson)
- [MatchStandaloneJSON](#matchstandalonejson) `New`
- [MatchYAML](#matchyaml) `New`
- [Matchers](#matchers)
- [match.Any](#matchany)
Expand Down Expand Up @@ -104,10 +105,9 @@ func TestSimple(t *testing.T) {
```

`go-snaps` saves the snapshots in `__snapshots__` directory and the file
name is the test file name with extension `.snap`.
name is the `t.Name()` plus a number plus the 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__/TestSimple_1.snaps`.
So for the above example the snapshot file name will be `./__snapshots__/TestSimple_1.snap` and `./__snapshots__/TestSimple_1.snap.html`.

## MatchJSON

Expand All @@ -131,6 +131,22 @@ func TestJSON(t *testing.T) {

JSON will be saved in snapshot in pretty format for more readability and deterministic diffs.

## MatchStandaloneJSON

`MatchStandaloneJSON` will create snapshots on separate files as opposed to `MatchJSON` which adds multiple snapshots inside the same file.

```go
func TestSimple(t *testing.T) {
snaps.MatchStandaloneJSON(t, `{"user":"mock-user","age":10,"email":"mock@email.com"}`)
snaps.MatchStandaloneJSON(t, User{10, "mock-email"})
}
```

`go-snaps` saves the snapshots in `__snapshots__` directory and the file
name is the `t.Name()` plus a number plus the extension `.snap.json`.

So for the above example the snapshot file name will be `./__snapshots__/TestSimple_1.snap.json` and `./__snapshots__/TestSimple_2.snap.json`.

## MatchYAML

`MatchYAML` can be used to capture data that can represent a valid yaml.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"age": 10,
"nested": {
"now": [
"<Any value>"
]
},
"user": "mock-user"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"age": "<less than 5 age>",
"email": "mock@email.com",
"user": "mock-user"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"email": "mock-user@email.com",
"keys": [
1,
2,
3,
4,
5
],
"name": "mock-user"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": "<Type:float64>"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"metadata": "<Type:map[string]interface {}>"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"age": 10,
"email": "mock@email.com",
"user": "mock-user"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"age": 10,
"email": "mock@email.com",
"user": "mock-user"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"mock-0": "value",
"mock-1": 2,
"mock-2": {
"Msg": "Hello World"
},
"mock-3": 10.4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"email": "mock@email.com",
"keys": [
1,
2,
3,
4,
5
],
"name": "mock-name"
}
16 changes: 0 additions & 16 deletions examples/__snapshots__/examples_test.snap

This file was deleted.

22 changes: 11 additions & 11 deletions examples/__snapshots__/matchJSON_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@
}
---

[TestMatchers/JSON_string_validation - 1]
{
"age": "<less than 5 age>",
"email": "mock@email.com",
"user": "mock-user"
}
---

[TestMatchers/Any_matcher/should_ignore_fields - 1]
{
"age": 10,
Expand All @@ -80,7 +72,15 @@
}
---

[TestMatchers/http_response - 1]
[TestMatchers/Custom_matcher/JSON_string_validation - 1]
{
"age": "<less than 5 age>",
"email": "mock@email.com",
"user": "mock-user"
}
---

[TestMatchers/Any_matcher/http_response - 1]
{
"data": {
"createdAt": "<Any value>",
Expand All @@ -89,13 +89,13 @@
}
---

[TestMatchers/type_matcher - 1]
[TestMatchers/Type_matcher/should_create_snapshot_with_type_placeholder - 1]
{
"data": "<Type:float64>"
}
---

[TestMatchers/type_matcher - 2]
[TestMatchers/Type_matcher/should_create_snapshot_with_type_placeholder - 2]
{
"metadata": "<Type:map[string]interface {}>"
}
Expand Down
56 changes: 56 additions & 0 deletions examples/__snapshots__/matchSnapshot_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,59 @@ map[string]interface {}{
[TestMatchSnapshot/withConfig - 1]
this should use the default config
---

[TestUpdateWithFlag/test_-_0 - 1]
lore ipsum dolor sit amet
---

[TestUpdateWithFlag/test_-_1 - 1]
consectetur adipiscing elit
---

[TestUpdateWithFlag/test_-_2 - 1]
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
---

[TestUpdateWithFlag/test_-_3 - 1]
Ut enim ad minim veniam, quis nostrud laboris nisi ut aliquip ex ea commodo consequat.
---

[TestParallel/should_snap_an_integer - 1]
int(10)
---

[TestParallel/should_snap_an_integer_slice - 1]
[]int{1, 2, 3, 4}
---

[TestParallel/should_snap_a_struct - 1]
struct { user string; email string; age int }{user:"gkampitakis", email:"mock@mail.com", age:10}
---

[TestParallel/should_snap_a_float - 1]
float64(10.5)
---

[TestParallel/should_snap_a_buffer - 1]
&bytes.Buffer{
buf: {0x42, 0x75, 0x66, 0x66, 0x65, 0x72, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67},
off: 0,
lastRead: 0,
}
---

[TestParallel/should_snap_a_map - 1]
map[string]int{"value-0":0, "value-1":1, "value-2":2, "value-3":3}
---

[TestParallel/should_snap_a_struct_with_fields - 1]
struct { _ struct {}; name string; id string }{
_: struct {}{},
name: "mock-name",
id: "123456",
}
---

[TestParallel/should_snap_a_pointer - 1]
&int(10)
---
40 changes: 0 additions & 40 deletions examples/__snapshots__/parallel_test.snap

This file was deleted.

30 changes: 0 additions & 30 deletions examples/examples_test.go

This file was deleted.

Loading

0 comments on commit 560fde0

Please sign in to comment.