Skip to content

[Bug]: Difference in diffs between MatchSnapshot and MatchStandaloneSnapshot #138

Closed

Description

@artuross

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:1

As you can see the output from MatchStandaloneSnapshot is much better showing only the line that actually changed.

Steps to Reproduce

  1. 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))
  1. go test ./...
  2. Change requestBody (e.g. add a character to one of the values).
  3. go test ./...

Expected Behavior

Both snapshots should only show the line that changed.

Activity

  1. gkampitakis commented on Sep 18, 2025

    @gkampitakis
    Owner

    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 because

    snaps.MatchSnapshot(t, string(requestDump))
    

    contains crlf characters that get dropped when reading the snapshot from the file.

    if you change your code to this

    snaps.MatchSnapshot(t, strings.ReplaceAll(string(requestDump), "\r\n", "\n"))
    

    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.

  2. artuross commented on Sep 20, 2025

    @artuross
    Author

    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!

  3. G-Rath commented on Nov 27, 2025

    @G-Rath
    Contributor

    @gkampitakis hate to say it, but this has caused a regression for us - both osv-scanner and osv-detector are now failing their Windows snapshots as it cannot find the snaps, which doesn't happen with this reverted:

  4. gkampitakis commented on Nov 27, 2025

    @gkampitakis
    Owner

    ahhh dammit. Sorry reverting this asap. I have windows as target on go-snaps tests but didn't caught the regression.

  5. gkampitakis commented on Nov 27, 2025

    @gkampitakis
    Owner

    I did a wrong tag but 0.5.18 has the revert. Again sorry will have a look why I missed the windows :/

  6. G-Rath commented on Nov 27, 2025

    @G-Rath
    Contributor

    @gkampitakis it's because you're turning off autocrtl in your Windows tests - see #147

  7. G-Rath commented on Dec 2, 2025

    @G-Rath
    Contributor

    @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.18 tag on the same commit and everything should be sorted

  8. gkampitakis commented on Dec 3, 2025

    @gkampitakis
    Owner

    This happens when you try to release first thing in the morning 🫠 Now should be fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions