Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to clean up unused snapshots? #29

Open
nene opened this issue Jan 8, 2018 · 4 comments
Open

How to clean up unused snapshots? #29

nene opened this issue Jan 8, 2018 · 4 comments

Comments

@nene
Copy link

nene commented Jan 8, 2018

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
Copy link
Owner

suchipi commented Jan 8, 2018

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
Copy link
Collaborator

@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 because updateSnapshot: "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
Copy link
Owner

suchipi commented Mar 15, 2018

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
Copy link
Collaborator

@suchipi sorry for late response.
Created a PR with the idea #33
Needs some more work but would be nice to get input from you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants