Awesome Open Source
Awesome Open Source

Cypress Visual Regression


github actions

Module for adding visual regression testing to Cypress.

Getting Started


$ npm install cypress-visual-regression

Add the following config to your cypress.json file:

  "screenshotsFolder": "./cypress/snapshots/actual",
  "trashAssetsBeforeRuns": true

Add the plugin to cypress/plugins/index.js:

const getCompareSnapshotsPlugin = require('cypress-visual-regression/dist/plugin');

module.exports = (on, config) => {
  getCompareSnapshotsPlugin(on, config);

Add the command to cypress/support/commands.js:

const compareSnapshotCommand = require('cypress-visual-regression/dist/command');


Make sure you import commands.js in cypress/support/index.js:

import './commands'


failSilently is enabled by default. Add the following config to your cypress.json file to see the errors:

  "env": {
    "failSilently": false

You can also pass default arguments to compareSnapshotCommand():

const compareSnapshotCommand = require('cypress-visual-regression/dist/command');

  capture: 'fullPage'

These will be used by default when no parameters are passed to the compareSnapshot command.

Configure snapshot paths

You can control where snapshots should be located by setting two environment variables:

Variable Description
SNAPSHOT_BASE_DIRECTORY Directory of the base snapshots
SNAPSHOT_DIFF_DIRECTORY Directory for the snapshot diff

The actual directory always points to the configured screenshot directory.

To Use

Add cy.compareSnapshot('home'); in your tests specs whenever you want to test for visual regressions, making sure to replace home with a relevant name. You can also add an optional error threshold: Value can range from 0.00 (no difference) to 1.00 (every pixel is different). So, if you enter an error threshold of 0.51, the test would fail only if > 51% of pixels are different.

More examples:

Threshold Fails when
.25 > 25%
.30 > 30%
.50 > 50%
.75 > 75%


it('should display the login page correctly', () => {
  cy.compareSnapshot('login', 0.0);
  cy.compareSnapshot('login', 0.1);

You can target a single HTML element as well:


You can pass arguments as an object to cy.screenshot(), rather than just an error threshold, as well:

it('should display the login page correctly', () => {
  cy.compareSnapshot('login', {
    capture: 'fullPage',
    errorThreshold: 0.1

Looking for more examples? Review docker/cypress/integration/main.spec.js.

Take the base images:

$ ./node_modules/.bin/cypress run --env type=base --config screenshotsFolder=cypress/snapshots/base,testFiles=\"**/*regression-tests.js\"

# use comma separated format for multiple config commands
$ ./node_modules/.bin/cypress run \
  --env type=base \
  --config screenshotsFolder=cypress/snapshots/base,testFiles=\"**/*regression-tests.js\"

Find regressions:

$ ./node_modules/.bin/cypress run --env type=actual



Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (70,351
cypress (80
cypress-plugin (16