Repository navigation
[Bug]: Difference in diffs between MatchSnapshot and MatchStandaloneSnapshot #138
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Description
I have a very simple test case like this:
requestBody := map[string]string{
"email": "test@example.com",
"password": "password13",
}
body, err := json.Marshal(requestBody)
require.NoError(t, err)
req := httptest.NewRequest(http.MethodPost, "/sign-in", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
requestDump, err := httputil.DumpRequest(req, true)
require.NoError(t, err)
snaps.MatchSnapshot(t, string(requestDump))
snaps.MatchStandaloneSnapshot(t, string(requestDump))The gist is that I create a new request and dump the request in standard text representation. That representation is saved as a snapshot. I've noticed that there's difference between how MatchSnapshot prints the diff vs how MatchStandaloneSnapshot does it.
Here's an example output (in both cases, this is the only snapshot available in a file):
--- FAIL: TestHandleSignIn (0.00s)
--- FAIL: TestHandleSignIn/should_return_201_and_token_when_sign_in_succeeds (0.00s)
auth_sign_in_test.go:42:
- Snapshot - 5
+ Received + 5
- POST /sign-in HTTP/1.1
- Host: example.com
- Content-Type: application/json
-
- {"email":"test@example.com","password":"password12"}
+ POST /sign-in HTTP/1.1
+ Host: example.com
+ Content-Type: application/json
+
+ {"email":"test@example.com","password":"password13"}
at __snapshots__/auth_sign_in_test.snap:2
auth_sign_in_test.go:43:
- Snapshot - 1
+ Received + 1
Host: example.com
Content-Type: application/json
- {"email":"test@example.com","password":"password12"}
+ {"email":"test@example.com","password":"password13"}
at __snapshots__/TestHandleSignIn_should_return_201_and_token_when_sign_in_succeeds_1.snap:1As you can see the output from MatchStandaloneSnapshot is much better showing only the line that actually changed.
Steps to Reproduce
- Prepare a test like this:
requestBody := map[string]string{
"email": "test@example.com",
"password": "password12",
}
body, err := json.Marshal(requestBody)
require.NoError(t, err)
req := httptest.NewRequest(http.MethodPost, "/sign-in", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
requestDump, err := httputil.DumpRequest(req, true)
require.NoError(t, err)
snaps.MatchSnapshot(t, string(requestDump))
snaps.MatchStandaloneSnapshot(t, string(requestDump))go test ./...- Change
requestBody(e.g. add a character to one of the values). go test ./...
Expected Behavior
Both snapshots should only show the line that changed.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
gkampitakis commentedon Sep 18, 2025
Hey 👋 Thanks for opening this issue and sharing the detailed context. This is not a bug per se but a limitation of
MatchSnapshot. The reason this is happening is becausecontains crlf characters that get dropped when reading the snapshot from the file.
if you change your code to this
it works as expected if you need a workaround now.I might have a solution but it will need some further testing and validation to make sure it's not creating a regresion. Will create a draft pr to check if you are interested to test.
artuross commentedon Sep 20, 2025
Hey @gkampitakis, thanks for such a quick answer. I recall I saw it mentioned somewhere that this library doesn't support
\r\n, but tbh it probably would be good to still handle that.Your workaround is good enough for me now. Thanks!
G-Rath commentedon Nov 27, 2025
@gkampitakis hate to say it, but this has caused a regression for us - both
osv-scannerandosv-detectorare now failing their Windows snapshots as it cannot find the snaps, which doesn't happen with this reverted:gkampitakis commentedon Nov 27, 2025
ahhh dammit. Sorry reverting this asap. I have windows as target on go-snaps tests but didn't caught the regression.
gkampitakis commentedon Nov 27, 2025
I did a wrong tag but 0.5.18 has the revert. Again sorry will have a look why I missed the windows :/
G-Rath commentedon Nov 27, 2025
@gkampitakis it's because you're turning off
autocrtlin your Windows tests - see #147G-Rath commentedon Dec 2, 2025
@gkampitakis it looks like the version tag is incorrect, so Go doesn't consider it valid - I think you just need to create a new
v0.5.18tag on the same commit and everything should be sortedgkampitakis commentedon Dec 3, 2025
This happens when you try to release first thing in the morning 🫠 Now should be fine