meinaart / cypress-plugin-snapshots

Plugin for snapshot tests in Cypress.io
MIT License
491 stars 116 forks source link

Programmatic setting UpdateSnapshot to true via commandline (feat) #107

Open AnnaLohse opened 4 years ago

AnnaLohse commented 4 years ago

problem It is far too laborious to delete all the incorrect snapshot files manually when there have been changes. I want to be able to write an npm script that sets UpdateSnapshot to true and then runs my tests.

solution Pass environment variables like UpdateSnapshot from the command line.

Describe alternatives you've considered Set UpdateSnapshot in e2e/plugins/index.js, which then internally configures the Cypress Snapshot plugin.

module.exports = (on, config) => {
    require('cypress-plugin-snapshots/plugin').initPlugin(on, config);
    if (process.env.CYPRESS_SNAPSHOT_UPDATE) {
        const parseConfig = JSON.parse(config.env['cypress-plugin-snapshots']);
        parseConfig.updateSnapshots = process.env.CYPRESS_SNAPSHOT_UPDATE;
        config.env['cypress-plugin-snapshots'] = JSON.stringify(parseConfig);
    }
    return config;
};

Usage: CYPRESS_SNAPSHOT_UPDATE=true yarn run e2e:dev

jcasasg commented 4 years ago

Is there any update from this enhancement?

tehciolo commented 3 years ago
module.exports = (on, config) => {
    require('cypress-plugin-snapshots/plugin').initPlugin(on, config);
    if (process.env.CYPRESS_SNAPSHOT_UPDATE) {
        const parseConfig = JSON.parse(config.env['cypress-plugin-snapshots']);
        parseConfig.updateSnapshots = process.env.CYPRESS_SNAPSHOT_UPDATE;
        config.env['cypress-plugin-snapshots'] = JSON.stringify(parseConfig);
    }
    return config;
};

parseConfig.updateSnapshots = process.env.CYPRESS_SNAPSHOT_UPDATE;

I had to parseConfig.updateSnapshots = true; to make this work as process.env.CYPRESS_SNAPSHOT_UPDATE was actually holding the String "true", so the snapshots were not updating.