cypress-snapshot-verify v0.9.0
Cypress Snapshot Verify
A Cypress plugin that assists with visual regression testing by providing automatic and manual snapshot verification.
The term snapshot is used to represent the expected screenshot which all future runs will be compared against
Installation
npm install --save-dev cypress-snapshot-verify
Usage
describe('App Snapshots', () => {
it('base snapshot with generic name', () => {
cy.visit('/');
cy.get('.App').should('be.visible').matchesSnapshot();
});
it('snapshot with specific name and higher tolerance for diff', () => {
cy.visit('/');
cy.get('.App').should('be.visible').matchesSnapshot('screenshot', { maxDiffPercentage: 0.2 });
});
});Configuration
This plugin supports the following configuration values, set via the cypress-snapshot-verify env key in the cypress config. Example cypress.config.js:
const { defineConfig } = require('cypress');
const { initPlugin } = require('cypress-snapshot-verify');
module.exports = defineConfig({
env: {
'cypress-snapshot-verify': {
snapshotsFolder: '../docs/snapshots/',
},
},
e2e: {
setupNodeEvents(on, config) {
initPlugin(on, config);
return config;
},
},
});Also add the following lines to the support/e2e.js file:
import { addMatchSnapshotCommand } from 'cypress-snapshot-verify';
addMatchSnapshotCommand();Plugin Configuration
passNewSnapshots-booleanDefault:
falseAutomatically create the snapshot and pass the test if the snapshot doesn't exist. If
falsethe test will fail with an error if the snapshot doesn't exist.updateFailingSnapshots-booleanDefault:
falseAutomatically update a snapshot that would normally fail due to size change or % difference and pass the test
errorOnMatchingSnapshotNames-booleanDefault:
trueThrow an error if a snapshot has the same name as an already-existing one. Setting this to
falsewill overwrite the existing snapshot file.snapshotsFolder-stringDefault:
./cypress/snapshotsThe directory you want the snapshots to be placed in
maxDiffPercentage-numberDefault:
0.0Maximum diff percentage calculated between snapshot and actual before the test fails
serverEnabled-booleanDefault:
trueEnables a server for the plugin and the ability to update failing snapshots from a modal within the Cypress UI
serverHost-stringDefault:
localhostServer hostname for plugin's server to attempt to bind to
serverPort:numberDefault:
2121Server port for plugin's server to attempt to bind to
Testing
Testing is done via Cypress tests defined in the cypress folder. Build the library first then run the Cypress tests.
npm run build
npm run test