jaredpalmer / cypress-image-snapshot

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

fix(command): prevent executing `Cypress.*`-code before cypress initialization #250

Closed nsbarsukov closed 1 year ago

nsbarsukov commented 2 years ago

Hi! I am a user of your plugin. I try to write my own plugin using your plugin as a basis. But I have faced with this problem.

Problem: Imagine that you have plugin.js-file:

import {
    addMatchImageSnapshotPlugin,
    matchImageSnapshotPlugin,
} from 'cypress-image-snapshot/plugin';

export function tuiAddSnapshotPlugin(on, config) {
    addMatchImageSnapshotPlugin(on, config);
    // ...
    on('after:screenshot', details => {
        // ...
        return matchImageSnapshotPlugin(details);
    })
}

And you have this command.js-file:

import {addMatchImageSnapshotCommand} from 'cypress-image-snapshot/command';

export function tuiAddMatchImageSnapshotCommand(options) {
    // ... some new logic
    addMatchImageSnapshotCommand(options);
}

Finally, everything is wrapped by index.js-file:

export * from './command';
export * from './plugin';

When you try to import plugin and command from your index.js and use it inside your cypress-tests, you will get error:

ReferenceError: Cypress is not defined

This happens because exporting of the plugin happens earlier than command (when Cypress is not yet ready). But some code (the changed lines of this PR) is not isolated by any function scope and we get the error. My PR will help to solve this problem without changing any logic of your code.

changeset-bot[bot] commented 2 years ago

⚠️ No Changeset found

Latest commit: c35beb552763e46eb3c23d6b0e4b5e64040cd7ff

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

nsbarsukov commented 2 years ago

@jaredpalmer could you review my contribution, please ?