jaredpalmer / cypress-image-snapshot

Catch visual regressions in Cypress
MIT License
888 stars 160 forks source link

Allow customizing snapshot subdirectory #158

Closed malykhinvi closed 3 years ago

malykhinvi commented 3 years ago

Basically, this change is a workaround for https://github.com/jaredpalmer/cypress-image-snapshot/issues/61.

Because of the existing behavior of Cypress,cy.screenshot in the Interactive mode puts screenshots in different subdirectories. If you run all specs the screenshot will be nested in 'All Specs' folder, whilst if you run the only spec file, the screenshot will be under the folder of a spec file name.

As this plugin works on top of cypress screenshots but uses its own folder to store snapshots (basically it copy/pastes screenshots to snapshots folder), one of the possible workarounds is to modify the subdirectory of every snapshot. To make it possible, we can pass a callback to plugin options like that:

const { addMatchImageSnapshotPlugin } = require('cypress-image-snapshot/plugin');

module.exports = (on, config) => {
  const getSpecSnapshotFolder = (specScreenshotFolder) => '';
  addMatchImageSnapshotPlugin(on, config, {getSpecSnapshotFolder});
};

If we return an empty string from getSpecSnapshotFolder, that effectively means that no subdirectory needed and all snapshots will be stored in the root of the snapshots folder. Not sure, what happens with spec names collisions though...

changeset-bot[bot] commented 3 years ago

⚠️ No Changeset found

Latest commit: 6c288dbe4b5de39effa7d2ae7741bc1a6e451ed1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

malykhinvi commented 3 years ago

@jaredpalmer hi! Could you please review and let me know what do you think of this workaround? After I resolved conflicts, I noticed a message from changeset-bot. I'm wondering, who should be changing the content for changelog?