suchipi / chai-jest-snapshot

Chai assertion that provides Jest's snapshot testing
MIT License
103 stars 17 forks source link

How to clean up unused snapshots? #29

Open nene opened 6 years ago

nene commented 6 years ago

I'd like to:

  1. Find if there are any unused snapshots.
  2. Remove those snapshots and remove the entire .snap files when no more snapshots left.

It seems that there is no way to accomplish it with this tool. Is there perhaps some other companion tool to help with that?

Alternatively, could such a support be added to chai-jest-snapshot? I could help out with implementation...

I noticed that the imported SnapshotState class has removeUncheckedKeys() method, which seems to be doing exactly what is needed. But I guess one can't just call it from the matchSnapshot() function in buildMatchSnapshot.js, as then it would remove all the snapshots besides the current one that's tested.

suchipi commented 6 years ago

You could probably call removeUncheckedKeys() in an "after all" hook. Assuming that works, we should add an API to chai-jest-snapshot that calls that, and add it to the recommended configurations in the README.

andrestaht commented 6 years ago

@suchipi tried the approach you suggested but ran into couple of problems.

matchSnapshot() creates new SnapshotState every time it is called. This means that only checked test will be current one that is being matched and removeUncheckedKeys() will remove all other snapshots from the file.

To solve it I created a snapshotStateHandler that holds state of the snapshot files and after each matchSnapshot() updates the state so at the end we know exactly what snapshots were unchecked for each file.

Another problem is that removeUncheckedKeys() will only work when CHAI_JEST_SNAPSHOT_UPDATE_ALL is set to true. Take a look at: https://github.com/facebook/jest/blob/master/packages/jest-snapshot/src/State.js#L104

Also after keeping the state inside separate handler the SnapshotState is created once at the beginning and therefor current matchSnapshot(true) updating would not work becauseupdateSnapshot: "new" has been set before.

Which means that the only way to update snapshots is to use CHAI_JEST_SNAPSHOT_UPDATE_ALL=true.

And the last issue comes back to matchSnapshot(). When snapshot tests are removed from some test file then matchSnapshot() is not being called no more and no SnapshotState is being created for that file and therefor nothing is checked and removed.

I hope my explanation is understandable. Any ideas how we could solve these problems? :)

If needed I can create a PR with my current changes.

suchipi commented 6 years ago

And the last issue comes back to matchSnapshot(). When snapshot tests are removed from some test file then matchSnapshot() is not being called no more and no SnapshotState is being created for that file and therefor nothing is checked and removed.

This issue could probably be solved with a beforeAll hook so that we create a SnapshotState for every file.

The other issues sound like they would need changes in jest-snapshot though. I think they'd be okay with making removeUncheckedKeys callable when we're not in update mode, but the matchSnapshot(true) thing is kinda chai-jest-snapshot-specific, so I'm not sure it makes sense for them to support it (they use the first argument to toMatchSnapshot for snapshot name in jest).

A PR would be welcome but it will be a while before I have some time to look at it.

andrestaht commented 6 years ago

@suchipi sorry for late response. Created a PR with the idea https://github.com/suchipi/chai-jest-snapshot/pull/33 Needs some more work but would be nice to get input from you :)