Gathering detailed insights and metrics for @caplin/cypress-image-snapshot
Gathering detailed insights and metrics for @caplin/cypress-image-snapshot
Gathering detailed insights and metrics for @caplin/cypress-image-snapshot
Gathering detailed insights and metrics for @caplin/cypress-image-snapshot
npm install @caplin/cypress-image-snapshot
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
Node Version
NPM Version
184 Commits
1 Watching
65 Branches
22 Contributors
Updated on 18 Jun 2022
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-24.2%
1,089
Compared to previous day
Last week
19.6%
7,163
Compared to previous week
Last month
7.9%
28,415
Compared to previous month
Last year
-46.1%
383,810
Compared to previous year
1
Cypress Image Snapshot binds jest-image-snapshot's image diffing logic to Cypress.io commands. The goal is to catch visual regressions during integration tests.
When using cypress open
, errors are displayed in the GUI.
When an image diff fails, a composite image is constructed.
When using cypress run
and --reporter cypress-image-snapshot/reporter
, diffs are output to your terminal.
Install from npm
1npm install --save-dev cypress-image-snapshot
then add the following in your project's <rootDir>/cypress/plugins/index.js
:
1const { 2 addMatchImageSnapshotPlugin, 3} = require('cypress-image-snapshot/plugin'); 4 5module.exports = (on, config) => { 6 addMatchImageSnapshotPlugin(on, config); 7};
and in <rootDir>/cypress/support/commands.js
add:
1import { addMatchImageSnapshotCommand } from 'cypress-image-snapshot/command'; 2 3addMatchImageSnapshotCommand();
1// addMatchImageSnapshotPlugin
2addMatchImageSnapshotPlugin(on, config);
3
4// addMatchImageSnapshotCommand
5addMatchImageSnapshotCommand();
6addMatchImageSnapshotCommand(commandName);
7addMatchImageSnapshotCommand(options);
8addMatchImageSnapshotCommand(commandName, options);
9
10// matchImageSnapshot
11.matchImageSnapshot();
12.matchImageSnapshot(name);
13.matchImageSnapshot(options);
14.matchImageSnapshot(name, options);
15
16// ---or---
17
18cy.matchImageSnapshot();
19cy.matchImageSnapshot(name);
20cy.matchImageSnapshot(options);
21cy.matchImageSnapshot(name, options);
1describe('Login', () => { 2 it('should be publicly accessible', () => { 3 cy.visit('/login'); 4 5 // snapshot name will be the test title 6 cy.matchImageSnapshot(); 7 8 // snapshot name will be the name passed in 9 cy.matchImageSnapshot('login'); 10 11 // options object passed in 12 cy.matchImageSnapshot(options); 13 14 // match element snapshot 15 cy.get('#login').matchImageSnapshot(); 16 }); 17});
Run Cypress with --env updateSnapshots=true
in order to update the base image files for all of your tests.
Run Cypress with --env failOnSnapshotDiff=false
in order to prevent test failures when an image diff does not pass.
Run Cypress with --reporter cypress-image-snapshot/reporter
in order to report snapshot diffs in your test results. This can be helpful to use with --env failOnSnapshotDiff=false
in order to quickly view all failing snapshots and their diffs.
If you using iTerm2, the reporter will output any image diffs right in your terminal 😎.
Similar use case to: https://github.com/cypress-io/cypress-example-docker-circle#spec--xml-reports
If you want to report snapshot diffs as well as generate XML junit reports, you can use mocha-multi-reporters.
npm install --save-dev mocha mocha-multi-reporters mocha-junit-reporter
You'll then want to set up a cypress-reporters.json
which may look a little like this:
1{ 2 "reporterEnabled": "spec, mocha-junit-reporter, cypress-image-snapshot/reporter", 3 "mochaJunitReporterReporterOptions": { 4 "mochaFile": "cypress/results/results-[hash].xml" 5 } 6}
where reporterEnabled
is a comma-separated list of reporters.
You can then run cypress like this:
cypress run --reporter mocha-multi-reporters --reporter-options configFile=cypress-reporters.json
or add the following to your cypress.json
{
..., //other options
"reporter": "mocha-multi-reporters",
"reporterOptions": {
"configFile": "cypress-reporters.json"
}
}
customSnapshotsDir
: Path to the directory that snapshot images will be written to, defaults to <rootDir>/cypress/snapshots
.customDiffDir
: Path to the directory that diff images will be written to, defaults to a sibling __diff_output__
directory alongside each snapshot.Additionally, any options for cy.screenshot()
and jest-image-snapshot can be passed in the options
argument to addMatchImageSnapshotCommand
and cy.matchImageSnapshot()
. The local options in cy.matchImageSnapshot()
will overwrite the default options set in addMatchImageSnapshot
.
For example, the default options we use in <rootDir>/cypress/support/commands.js
are:
1addMatchImageSnapshotCommand({
2 failureThreshold: 0.03, // threshold for entire image
3 failureThresholdType: 'percent', // percent of image or number of pixels
4 customDiffConfig: { threshold: 0.1 }, // threshold for each pixel
5 capture: 'viewport', // capture viewport in screenshot
6});
We really enjoy the diffing workflow of jest-image-snapshot and wanted to have a similar workflow when using Cypress. Because of this, under the hood we use some of jest-image-snapshot's internals and simply bind them to Cypress's commands and plugins APIs.
The workflow of cy.matchImageSnapshot()
when running Cypress is:
cy.screenshot()
named according to the current test.<rootDir>/cypress/snapshots
and if so diff against that snapshot.<rootDir>/cypress/snapshots/__diff_output__
.Cypress's screenshot functionality has changed significantly across 3.x.x
versions. In order to avoid buggy behavior, please use the following version ranges:
cypress-image-snapshot@>=1.0.0 <2.0.0
for cypress@>=3.0.0 <3.0.2
cypress-image-snapshot@>2.0.0
for cypress@>3.0.2
.No vulnerabilities found.
No security vulnerabilities found.