Skip to content

Commit 67798ec

Browse files
committed
add a readme
1 parent 2e5527b commit 67798ec

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# gocksnap [![GitHub release](https://img.shields.io/badge/version-v0.1-green.svg?style=flat)](https://github.com/millotp/gocksnap/releases)
2+
3+
Jest-like snapshot built on top of [gock](https://github.com/h2non/gock).
4+
5+
It allows you to record HTTP request for a test, select the response for each one and replay the snapshot in the test.
6+
7+
## Example
8+
9+
```go
10+
func TestWithHTTP(t *testing.T) {
11+
defer gock.off()
12+
13+
snapshot := gocksnap.MatchSnapshot(t, "name of the scenario")
14+
15+
// put you code here that calls HTTP endpoint
16+
// ...
17+
18+
// call finish to save the snapshot and assert that the test is complete
19+
snapshot.Finish(t)
20+
}
21+
```
22+
23+
## Preview
24+
25+
When running the test for the first time, the library will open a web server to select the response for each request:
26+
27+
<img width="1040" height="871" alt="Screenshot 2025-08-20 at 19 10 35" src="https://github.com/user-attachments/assets/779bcb86-5943-4bfb-9e25-f0218e5595b3" />
28+
29+
Select the desired `status` and `response body` that should be returned for this request, and click `Save Snapshot`.
30+
31+
## Updating a snapshot
32+
33+
To edit an existing snapshot, you can run your test with the environment variable `UPDATE_GOCKSNAP=true`, a new button will appear to reuse the response from the existing snapshot:
34+
35+
<img width="1039" height="334" alt="Screenshot 2025-08-20 at 19 13 16" src="https://github.com/user-attachments/assets/5105b67f-3509-4ac3-817a-423d34498e1a" />

gocksnap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (g *Snapshot) promptCall(req *http.Request, existingCall *Call) *Call {
128128
}
129129

130130
// MatchSnapshot creates a new snapshot for the current test.
131-
// If the snapshot file is not found, or if the environment variable UPDATE_TESTS is set to "true", it will spawn a web server to allow the user to interactively select responses for the recorded requests.
131+
// If the snapshot file is not found, or if the environment variable UPDATE_GOCKSNAP is set to "true", it will spawn a web server to allow the user to interactively select responses for the recorded requests.
132132
// If the snapshot file is found, it will load the existing calls and register them with gock.
133133
// After all the calls are finished, the user should call the Finish method to save the snapshot / assert that all calls were mocked correctly.
134134
func MatchSnapshot(t *testing.T, snapshotName string) *Snapshot {
@@ -138,7 +138,7 @@ func MatchSnapshot(t *testing.T, snapshotName string) *Snapshot {
138138
Calls: []Call{},
139139
testName: t.Name(),
140140
name: snapshotName,
141-
updateMode: os.Getenv("UPDATE_TESTS") == "true",
141+
updateMode: os.Getenv("UPDATE_GOCKSNAP") == "true",
142142
sseConns: make(map[chan string]struct{}),
143143
}
144144

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ <h1 id="title">Gocksnap Console</h1>
157157
}
158158

159159
function showForm(data) {
160-
document.getElementById('title').textContent = "Snapshot for " + data.name;
160+
document.getElementById('title').textContent = "Snapshot for: " + data.name;
161161
let html = `
162162
<div>
163163
<label>Incoming request:</label>

0 commit comments

Comments
 (0)