gkampitakis / go-snaps

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

[Feature Request]: auto-updated inline snaps #110

Open ottaviohartman opened 1 month ago

ottaviohartman commented 1 month ago

🚀 Feature Proposal

cargo insta has this very cool feature called inline snapshots. When a snapshot updates, it updates the code itself. How hard would this be to implement in Go?

It could be based on some //go: marker above the check, possibly? And maybe it would need to be an option in TestMain.

Motivation

Seeing, inline, the expected values of unit tests instead of hopping between files and scanning multiple lines of output.

Example

Not sure yet, perhaps using //go:generate? The crazy part is Rust somehow generates the code inside the file, rather than writing a new file. I'm not sure if this breaks certain Go workflows.

// before compilation
// `snapshot-inline`, or some marker, tells the codegen where to inject

// snapshot-inline
snaps.MatchSnapshot(t, rr.Code, "")
// after compilation

// snapshot-inline
snaps.MatchSnapshot(t, rr.Code, 201)
gkampitakis commented 1 month ago

Hey 👋 Thanks for opening this issue. I have explored this idea in the past. You can see this pull request here experimenting with inline snapshot support.

I was able to make it work quite well but

  1. I wasn't able to find a way to split long inline strings to multiple lines ( I don't remember how jest handles this)
  2. The solution I found at least some times messes up the comments. ( again I don't remember the specifics)
  3. I didn't know if this is something that people would be interested in, if there was value on pursuing it.

If that's something that you would find useful, I can try and unearth that pr and share more details of limitations and/or feasibility.

ottaviohartman commented 1 month ago

I'd find value! If I have time I can work on this too. Maybe even having it under some experimental flag would be nice while I use in my projects.

gkampitakis commented 1 month ago

Okay early next week will have some time to refresh my memory on this and I can share a branch for you to test and see from there where we can go with this feature. But I am not against it as long as it's useful and stable (even if it has limitations).