avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Allow updating snapshots for individual tests or individual assertions #2635

Closed ninevra closed 3 years ago

ninevra commented 3 years ago

What you're trying to do

Example use case:

Why you can't use AVA for this

AVA appears to provide no way to select individual tests or assertions for update. Selecting files can be accomplished by passing AVA a glob, but using --match or line number selection with --update-snapshots throws.

Fundamentally, this is because AVA implements --update-snapshots by entirely ignoring and overwriting the snapshot files. If AVA allowed users to select specific tests, then every other test in their files would have its snapshots deleted.

And maybe how you think AVA could handle this

From a ui perspective, AVA could handle this by providing an interactive --review-snapshots option, like the similar features in insta (implemented as a cli tool, cargo insta review) or jest (seemingly only available during watch mode).

AVA could also allow the use of --match and other existing forms of selection with --update-snapshots, but this would only allow per-test granularity, not per-snapshot.

From an implementation perspective, AVA would probably need to read snapshot files from disk even when run with --update-snapshots, so that the unselected tests/assertions could retain their preexisting snapshots.

It might be possible to implement this using AVA's currently-unused ability to append updates to .snap files:

https://github.com/avajs/ava/blob/3fe4c40f09fd8eef74ea998992a756525fca7b9f/lib/snapshot-manager.js#L184-L186

https://github.com/avajs/ava/blob/3fe4c40f09fd8eef74ea998992a756525fca7b9f/lib/snapshot-manager.js#L298-L299

but I think that would require (backwards-compatible!) changes to the snapshot file format.

novemberborn commented 3 years ago

If we knew the test was failing because of the snapshot, then we could retain that failing snapshot while updating everything else. We'd have to be able to regenerate the Markdown file based on the binary snapshot serialization, which should be possible.