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]: Expose ability to update snapshot via the API #77

Closed wagoodman closed 1 year ago

wagoodman commented 1 year ago

πŸš€ Feature Proposal

It would be great to be able to update snapshots more programmatically:

    cfg := snaps.WithConfig(snaps.Update(true))
    cfg.MatchSnapshot(t, myTestSubject)

In this way you could have other test conditions / mechanisms that enforce when a snapshot can and should be updated.

Motivation

Today we use the flag package which hooks nicely into the standard test utils:

package spdxjson

import (
    "flag"
    "testing"
)

var updateSnapshot = flag.Bool("update-spdx-json-snapshot", false, "update the *.golden files for spdx-json encoders")

func Test_Something(t *testing.T) {
    ...
    if *updateSnapshot {
        // do something...
    }
    ...
}

Which can be used like so:

go test ./path/to/package -update-spdx-json-snapshot

See:

This has the nice quality that:

Example

Combining the motivation and the feature request:

package spdxjson

import (
    "flag"
    "testing"
)

var updateSnapshot = flag.Bool("update-spdx-json-snapshot", false, "update the *.golden files for spdx-json encoders")

func Test_Something(t *testing.T) {
    ...
    cfg := snaps.WithConfig(snaps.Update(*updateSnapshot))
    cfg.MatchSnapshot(t, myTestSubject)
    ...
}

In which case the flags are the only way to update snapshot files.

gkampitakis commented 1 year ago

hey πŸ‘‹ thanks a lot for opening this issue. I really like this idea. I created a draft pr to support this. I am thinking that the priority should be CI > snaps.Update > UPDATE_SNAPS . So if running on a CI the snaps won't update and will fail. wdyt ?

gkampitakis commented 1 year ago

Have added this on #78 please tell me if that works for you. Again thanks for opening this issue.