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

Vitest currentTestName causing ENAMETOOLONG #345

Open programmer24601 opened 6 months ago

programmer24601 commented 6 months ago

When using vitest 1.1.1 my snapshots are getting renamed to include the file path.

For example, a test file with:

Would, with jest, generate a snapshot filename: draw-chart-test-ts-draw-chart-should-generate-an-image-data-url-for-scenario-38-1-snap.png

Whereas with vitest re-running the test generates a new snapshot file with filename: draw-chart-test-ts-lib-chart-components-draw-chart-test-ts-draw-chart-should-generate-an-image-data-url-for-scenario-38-1-snap.png

Node is then falling over with ENAMETOOLONG error when running the tests from the root directory of my monorepo.

I thought the toMatchImageSnapshot() option customSnapshotIdentifier might help but reducing it to:

{
  customSnapshotIdentifier: (parameters) =>
    kebabCase(`${parameters.currentTestName}`)
}

Gives: lib-chart-components-draw-chart-test-ts-draw-chart-should-generate-an-image-data-url-for-scenario-38.png

Which tells me the Vitest API differs from Jest.

I'm pretty much blocked by this so help would be appreciated!

programmer24601 commented 6 months ago

Okay I've got a workaround:

customSnapshotIdentifier: (parameters) => {
  const [filePath, describeTitle, testTitle] =
    parameters.currentTestName.split(' > ');

  const filename = filePath.split('/').at(-1);

  return kebabCase(
    `${filename}-${describeTitle}-${testTitle}-${parameters.counter}-snap`
  );

I'd be nice if jest-image-snapshot could determine the testing framework and do this automatically for vitest.

programmer24601 commented 6 months ago

Raised issue on vitest: https://github.com/vitest-dev/vitest/issues/4874