americanexpress / jest-image-snapshot

✨ Jest matcher for image comparisons. Most commonly used for visual regression testing.
Apache License 2.0
3.81k stars 197 forks source link

[Feature request] Expose `onBeforeWriteToDisk` hook (to manipulate EXIF data) #332

Closed Ayc0 closed 7 months ago

Ayc0 commented 1 year ago

At the moment, any test can generate any snapshot, and it's really hard to be able to find which test generated which image (as we can provide custom paths for the images, we cannot just rely on the path of the images, and also if we have 2 tests files in the same folder, images can be put in the same folder).

We could use EXIF data in images to store metadata in them (and we could also add the possibility in .toMatchImageSnapshot() to provide extra EXIF data if we want too), like the test suite (with the describe / it), the test path etc.

Ayc0 commented 1 year ago

We can use https://www.npmjs.com/package/node-exiftool to help with this (this library can read & write exif)

Edit: this library requires another one to be installed top level. So this is a no go.

What about https://github.com/hometlt/png-metadata or https://github.com/kujirahand/node-png-metadata? it's not really about exif data but other kind of metadata. It only works for PNG but jest-image-snapshot also only supports them

Ayc0 commented 1 year ago

If that's something we want to do, I can propose a PR

Ayc0 commented 1 year ago

I look a bit into it, and manipulate EXIF can be tricky: there is plenty we can do and I don't think there is much we'd like to do from the jest-image-snapshot point of view.

One thing we could do is expose a hook for just before writing a file to the FS:

type OnBeforeWriteToDisk = (arguments: {
  buffer: Buffer; testPath: string; destination: string; /* others */
}) => Buffer

So that people could be free to do what they want with it, and while keeping the full control on how to write to disk

Ayc0 commented 1 year ago

I opened https://github.com/americanexpress/jest-image-snapshot/pull/337 to solve this