Gathering detailed insights and metrics for @simonsmith/cypress-image-snapshot
Gathering detailed insights and metrics for @simonsmith/cypress-image-snapshot
Gathering detailed insights and metrics for @simonsmith/cypress-image-snapshot
Gathering detailed insights and metrics for @simonsmith/cypress-image-snapshot
Catch visual regressions in Cypress with jest-image-snapshot
npm install @simonsmith/cypress-image-snapshot
Typescript
Module System
Node Version
NPM Version
TypeScript (49.56%)
HTML (26.06%)
CSS (16.6%)
JavaScript (6.7%)
Dockerfile (1.09%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
71 Stars
118 Commits
16 Forks
3 Watchers
3 Branches
8 Contributors
Updated on Apr 14, 2025
Latest Version
9.1.0
Package Id
@simonsmith/cypress-image-snapshot@9.1.0
Unpacked Size
31.50 kB
Size
10.28 kB
File Count
18
NPM Version
9.6.7
Node Version
18.20.3
Published on
Jul 16, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
1
27
Cypress Image Snapshot binds jest-image-snapshot's image diffing logic to Cypress.io commands.
jaredpalmer/cypress-image-snapshot
Install with your chosen package manager
1# yarn 2yarn add --dev @simonsmith/cypress-image-snapshot 3 4# npm 5npm install --save-dev @simonsmith/cypress-image-snapshot
Next, import the plugin function and add it to the setupNodeEvents
function:
1// cypress.config.ts 2 3import {defineConfig} from 'cypress' 4import {addMatchImageSnapshotPlugin} from '@simonsmith/cypress-image-snapshot/plugin' 5 6export default defineConfig({ 7 e2e: { 8 setupNodeEvents(on) { 9 addMatchImageSnapshotPlugin(on) 10 }, 11 }, 12})
Add the command to your relevant support file:
1// cypress/support/e2e.ts 2 3import {addMatchImageSnapshotCommand} from '@simonsmith/cypress-image-snapshot/command' 4 5addMatchImageSnapshotCommand() 6 7// can also add any default options to be used 8// by all instances of `matchImageSnapshot` 9addMatchImageSnapshotCommand({ 10 failureThreshold: 0.2, 11})
TypeScript is supported so any reference to @types/cypress-image-snapshot
can be removed from your project
Ensure that the types are included in your tsconfig.json
{
"compilerOptions": {
// ...
},
"types": ["@simonsmith/cypress-image-snapshot/types"]
}
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 // snapshot will be created inside `some/dir`
12 cy.matchImageSnapshot('some/dir/image')
13
14 // options object passed in
15 cy.matchImageSnapshot({
16 failureThreshold: 0.4
17 blur: 10
18 });
19
20 // match element snapshot
21 cy.get('#login').matchImageSnapshot();
22 });
23});
The options object combines jest-image-snapshot and Cypress screenshot configuration.
1cy.matchImageSnapshot({
2 // options for jest-image-snapshot
3 failureThreshold: 0.2,
4 comparisonMethod: 'ssim',
5
6 // options for Cypress.screenshot()
7 capture: 'viewport',
8 blackout: ['.some-element'],
9})
It is also possible to configure the extensions given to snap and diff files generated by the plugin. The default options are:
1{ 2 snapFilenameExtension: '.snap', 3 diffFilenameExtension: '.diff', 4}
1// will create a snap called `some-name.custom-snap-name.png`
2cy.matchImageSnapshot('some-name', {
3 snapFilenameExtension: '.custom-snap-name',
4})
5
6// will create a snap called `some-name.png`
7cy.matchImageSnapshot('some-name', {
8 snapFilenameExtension: '',
9})
10
11// will create a diff called `some-name.wrong.png` when a test fails
12cy.matchImageSnapshot('some-name', {
13 diffFilenameExtension: '.wrong',
14})
As of Cypress 10.0.0 a change was made to remove common ancestor paths of
generated screenshots. This means that it is difficult to mimic the folder
structure found in the cypress/e2e/
directory when creating the snapshots
directory.
To workaround this, cypress-image-snapshot makes use of a e2eSpecDir
option. Here's an example:
1addMatchImageSnapshotCommand({
2 e2eSpecDir: 'cypress/e2e/', // the default value
3})
Example output in a project:
cypress
├── e2e
│ ├── matchImageSnapshot.cy.ts
│ ├── nested
│ │ └── test
│ └── someOtherTest.cy.ts
├── snapshots
│ ├── matchImageSnapshot.cy.ts
│ │ ├── matches with just options.snap.png
│ │ ├── name and options.snap.png
│ │ ├── no arguments.snap.png
│ │ └── with custom name.snap.png
│ ├── nested
│ │ └── test
│ └── someOtherTest.cy.ts
│ └── some other test taking a snapshot.snap.png
Without the e2eSpecDir
option the cypress/e2e/
directories would be
repeated inside the snapshots
directory. Set this option to whatever
directory structure is inside the specPattern
configuration value.
See more:
Run Cypress with --env updateSnapshots=true
in order to update the base image files for all of your tests.
By default tests will fail when a snapshot fails to match. Run Cypress with
--env failOnSnapshotDiff=false
in order to prevent test failures when an image
diff does not pass.
Run Cypress with --env requireSnapshots=true
in order to fail if snapshots are missing. This is useful in continuous integration where snapshots should be present in advance.
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__
.Tested on Cypress 10.x, 11.x and 12.x
Cypress must be installed as a peer dependency
yarn install
package.json
To make it easier to test whilst developing there are a few simple Cypress tests that validate the plugin. There are two ways to run these tests:
yarn test:open
In open mode the tests run in Electron and ignore any snapshot failures. This is due to the rendering differences on developer machines vs CI. Here there is also verbose output sent to the test runner console to aid debugging.
Note here that the yarn script above will re-build the plugin each time. This is
necessary because the tests are run against the output in the dist
directory
to ensure parity between the built package on NPM.
Ensure that the command is run each time changes need to be tested in Cypress
yarn docker:build
yarn docker:run
The commands here ensure that the tests are run inside a Docker container that matches the CI machine. This allows images to be generated and matched correctly when running the tests in Github Actions.
It is necessary to have two environment variables defined by default before running the tests in Docker:
CYPRESS_updateSnapshots=false
CYPRESS_debugSnapshots=false
It's recommended that these are loaded into the shell with something like direnv
Then they can be overridden as needed:
CYPRESS_updateSnapshots=true yarn docker:run
jaredpalmer/cypress-image-snapshot
This is a rewrite of the original plugin as active development has ceased. Full credit goes to Jared Palmer.
6.5/10
Summary
@simonsmith/cypress-image-snapshothas fix for insecure snapshot file names
Affected Versions
<= 8.0.1
Patched Versions
8.0.2
No security vulnerabilities found.