Prior99 / jest-screenshot

A jest extension to deal with screenshots and all sorts of images.
MIT License
71 stars 21 forks source link

[Feature] Delete obsolete screenshots on update #10

Open k-g-a opened 5 years ago

k-g-a commented 5 years ago

Hi! There is a known limitation in both this addon and jest-image-snapshots: running jest -u won't delete obsolete screenshots. This is due to fact, that there is no source of information for runner about which screenshots must be used. One possible solution would be to create such source in form of json file which would be placed in snapshotsDir (default or specified by user). The scturcure of this file would be:

{
  "TEST_NAME_1 COUNTER": {
    "filePath": "PATH_TO_FILE_1",
    "meta": {}
  },
  "TEST_NAME_2 COUNTER": {
    "filePath": "PATH_TO_FILE_2",
    "meta": {}
  }
}

The rest of the logic could be taken from original toMatchSnapshot().

I propose to use testName => {filePath, meta} instead of testName => filePath, so one could store and read additional info in custom matchers by calling:

  import {getMetaFor} from 'jest-screenshots';
  ...
  const myMeta = getMetaFor(this);
  ...
  exprect(target).toMatchImageSnapshot({
    meta?: {...} // any serializable data
  })

Our particular use case is to store enzyme's .html() in meta and compare it with target.html() before taking actual screenshot for comparison. For now we keep all that logic in our custom matcher and it saves plenty of time for non-changed assertions (no need to wait for puppeteer). Another use case could be storing settings (antialiasing/threshholds) or environment (os/browser) used to take screenshot. It may seem over-complicated at first glance, but actual implementation details would be hidden for most users while other could benefit from it.

If this feature is anticipated I can make a PR.

kevinbuhmann commented 2 years ago

I solved reporting/deleting obsolete screenshot files with patch-package and this patch against version jest-screenshot@0.3.5.