Skip to content

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

@artuross

Description

@artuross

Description

I have a very simple test case like this:

		requestBody := map[string]string{
			"email":    "[email protected]",
			"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":"[email protected]","password":"password12"}
            + POST /sign-in HTTP/1.1
            + Host: example.com
            + Content-Type: application/json
            + 
            + {"email":"[email protected]","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":"[email protected]","password":"password12"}
            + {"email":"[email protected]","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":    "[email protected]",
			"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.

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