bradleyjkemp / cupaloy

Simple Go snapshot testing
MIT License
304 stars 26 forks source link

Support for JSON Snapshots #82

Open sazzer opened 2 years ago

sazzer commented 2 years ago

Currently the diffing tool used by Cupaloy treats strings as just a sequence of characters. Which is reasonable, but limited.

In particular, it would be fantastic if there were a way to tell it to treat the two strings as JSON. Which would mean:

That way, any changes to the output that are changes to the raw string but are not meaningful changes to the JSON structure are ignored and will not cause the tests to fail.

In particular, I've had an issue where VSCode auto-formats the JSON within a snapshot file when I'm making changes to it, which then means that everything breaks. Often it actually means it's quicker to delete the file and regenerate it rather than just fixing up one value that's changed because of a code change.

sazzer commented 2 years ago

In reality this would probably just mean changing:

    if snapshot == prevSnapshot || takeV1Snapshot(i...) == prevSnapshot {

in cupaloy.go to use a configured diffing function instead of ==. Users can then provide their own means to compare values - e.g. github.com/kinbiko/jsonassert, but maybe even much more complicated things for some cases like image snapshots or whatever.