gkampitakis / go-snaps

Jest-like snapshot testing in Go 📸
https://pkg.go.dev/github.com/gkampitakis/go-snaps
MIT License
159 stars 6 forks source link

[Bug]: mkdir /usr/local/go/src/runtime/__snapshots__: permission denied #107

Closed jackmatt2 closed 1 month ago

jackmatt2 commented 1 month ago

Description

Im my case I'm snapshotting HTTP requests in a stub server in order to check the calls I'm making are correctly formatted (headers, body etc.). I have this running as part of a integration test. The call to snaps.MatchJSON is made in the HTTP handler of the stub server.

It appears the algorithm that searches for the test folder skips over it (see screenshot). I can see it iterating the test method but the algorithm doesn't successfully return it.

Steps to Reproduce

Use snaps.MatchJSON outside the actual test function, for example in a stub server handler which the code is making HTTP requests to. Run the test and see the test fail when trying to create the snapshots folder.

go-snaps

Expected Behavior

Test should resolve the test method folder

gkampitakis commented 1 month ago

Hey :wave: thanks for opening this issue.

I don't believe this is a bug but more of a known limitation, but I am not sure how exactly your setup looks. If you can share a reproducible example happy to look if this is an edge case I missed.

There is a workaround for this though if you are interested to use. It's using an absoulute path where you override the baseCallermethod entitrely.

t.Run("should allow absolute path", func(t *testing.T) {
            _, b, _, _ := runtime.Caller(0)
            basepath := filepath.Dir(b)

            snaps.WithConfig(snaps.Dir(basepath+"/absolute_path"), snaps.Filename("my_test")).
                MatchSnapshot(t, "supporting absolute path")
        })

Also there might be a similar use case a user had and was adressed with the above if you want to read https://github.com/gkampitakis/go-snaps/pull/60#discussion_r1197658271

gkampitakis commented 1 month ago

@jackmatt2 closing this one as I see the 👍 . If you have more issues please either comment here or create a new issue, happy to help 😄