Closed HenrikPoulsen closed 1 year ago
Hey 👋 I have thought about this use case but no one asked for it. I have checked in the past what Jest was doing but I can't recall it's behaviour on top of my head.
The problem with go test is that every test is running independently from the other. So in order to support sorted insertions you have to either every time you are adding a new snap to scan the whole file and add it in the correct place (that could be slow as we need to use locks as well) or at the end grab the whole file and sort it. (The only way to know that you are at the end of the tests is from inside the TestMain)
If this is something that would be helpful for people then something that could work would be to change the snaps.Clean
. A rough idea would look like this. Registering the snaps.clean on Main and passing a sort true option.
func TestMain(t *testing.M) {
v := t.Run()
// After all tests have run `go-snaps` can check for not used snapshots
snaps.Clean(t, options{
sort: true
})
os.Exit(v)
}
What are your thoughts on this?
The suggestion of being able to get clean to also sort it would work for me at least. Since I already run clean for my go-snaps suites
@HenrikPoulsen I have this pr #80 ready. It mostly looks good I just want to run some more manual tests. Would you like to give it a try if it works for you?
go get github.com/gkampitakis/go-snaps@sort_snaps
Since I already run clean for my go-snaps suites
Also I am interested in your feedback on this? How does this work for you. Thanks again.
Issue
I find it useful for the snapshots to be sorted. In the scenario where you change something so a ton of snapshots are obsolete, but some of the tests actually find some bugs that need to be fixed. For my sanity I then prefer to just regenerate the snapshots that are just obsolete and leave the ones that are broken. The easiest way in my mind to accomplish that is to just delete the snapshots that are outdated and let them be regenerated. However the order of the snapshots changes when that is done so the diff in git will be larger than is necessary. It seems like go-snaps just appends any new snapshots to the end of the file
I imagine that the intended way to do this is to run each test manually with UPDATE_SNAPS set, but that's more time consuming in these cases than just modifying the snapshot directly.
Would adding sorting or some other mechanism to make this usecase easier be reasonable? Jest snapshots for example are sorted from my experience so I can do exactly what I outlined above