npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-visual-regression-hirsch

v4.0.1

Published

Module for adding visual regression testing to Cypress

Downloads

4

Readme

Cypress Visual Regression

npm

github actions

Module for adding visual regression testing to Cypress.

Installation

npm install cypress-visual-regression

Configuration

JavaScript

Configure the visual regression plugin and environment variables in your cypress.config.js file like:

const { defineConfig } = require("cypress")
const { configureVisualRegression } = require('cypress-visual-regression')

module.exports = defineConfig({
  e2e: {
    env: {
      visualRegression: {
        type: 'regression'
      }
    },
    screenshotsFolder: './cypress/snapshots/actual',
    setupNodeEvents(on, config) {
      configureVisualRegression(on)
    }
  }
})

Pay attention to the type option. Use 'base' to generate baseline images, and 'regression' to compare current screenshot to the base screenshot

In your support file cypress/support/e2e.js add the following:

const { addCompareSnapshotCommand } = require('cypress-visual-regression/dist/command')
addCompareSnapshotCommand()

TypeScript

If you're using TypeScript, use files with a .ts extension, as follows:

cypress.config.ts

import { defineConfig } from 'cypress'
import { configureVisualRegression } from 'cypress-visual-regression/dist/plugin'

export default defineConfig({
  e2e: {
    env: {
      visualRegression: {
        type: 'regression'
      }
    },
    screenshotsFolder: './cypress/snapshots/actual',
    setupNodeEvents(on, config) {
      configureVisualRegression(on)
    },
  },
})

cypress/support/e2e.ts

import { addCompareSnapshotCommand } from 'cypress-visual-regression/dist/command'
addCompareSnapshotCommand()

cypress/tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "types": [
      "cypress",
      "cypress-visual-regression"
    ]
  }
}

For more info on how to use TypeScript with Cypress, please refer to this document.

Plugin options

All options can be configured within visualRegression under env variable inside cypress.config.js file, like this:

e2e: {
  screenshotsFolder: './cypress/snapshots/actual',
  env: {
    visualRegression: {
      type: 'regression',
      baseDirectory: 'cypress/snapshot/base',
      diffDirectory: 'cypress/snapshot/diff',
      generateDiff: 'always',
      failSilently: true
    }
  }
}

| Variable | Default | Description | |---------------|-------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------| | type | / | Either 'regression' or 'base'. Base will override any existing base images with new screenshots. Regression will compare the base to the current screenshot. | | baseDirectory | 'cypress/snapshot/base' | Path to the directory where the base snapshots will be stored. | | diffDirectory | 'cypress/snapshot/diff' | Path to the directory where the generated image differences will be stored. | | generateDiff | 'fail' | Either 'fail', 'never' or 'always'. Determines if and when image differences are generated. | | failSilently | false | Used to decide if any error found in regression should be thrown or returned as part of the result. |

You can also pass default cypress screenshot arguments to addCompareSnapshotCommand(), like this:

const addCompareSnapshotCommand = require('cypress-visual-regression/dist/command')
addCompareSnapshotCommand({
  capture: 'fullPage'
})

How To Use

> syntax

cy.compareSnapshot(name)
cy.compareSnapshot(name, errorThreshold)
cy.compareSnapshot(name, options)

> arguments

| Arguments | Default | Description | |----------------|---------|--------------------------------------------------------------------------------------------------------------| | name | / | Represents the name of the base snapshot file that the actual screenshot will be compared with. | | errorThreshold | 0 | Threshold under which any image difference will be considered as failed test. Represented in percentages. | | options | {} | Used to provide additional cypress screenshot options as well as failSilently and errorThreshold values. |

> examples

cy.compareSnapshot('homePage') // will compare actual screenshot to current and fail if there's any difference in the images

cy.get('h1').compareSnapshot('homePage', 0.2) // will compare only the image of h1 element and fail only if the percentage of pixels that are different is bigger than 0.2%

cy.compareSnapshot('homePage', {errorThreshold: 1, failSilently: true}).then(comparisonResults => {
  console.log(comparisonResults.mismatchedPixels) // will print the number of mismatched pixels
  console.log(comparisonResults.percentage) // will print the percentage of mismatched pixels
  console.log(comparisonResults.error) // will print the visual regression error message (if any)
})

Looking for more examples? See cypress/e2e/main.cy.ts.

Example

example

Tips & Tricks

Ignore some elements

Following function creates a command that allows you to hide elements of the page based on their className:

/**
 * To be called after you setup the command, in order to add a
 * hook that does stuff before the command is triggered
 */
export function beforeCompareSnapshots(
  /** Element you want to ignore */
  ignoredElementsQuerySelector: string,
  /** Main app element (if you want for the page to be loaded before triggering the command) */
  appContentQuerySelector: string = "body"
) {
  Cypress.Commands.overwrite("compareSnapshots", (originalFn, ...args) => {
    return cy
      // wait for content to be ready
      .get(appContentQuerySelector)
      // hide ignored elements
      .then($app => {
        return new Cypress.Promise((resolve, reject) => {
          setTimeout(() => {
            $app.find(ignoredElementsQuerySelector).css("visibility", "hidden");
            resolve();
            // add a very small delay to wait for the elements to be there, but you should
            // make sure your test already handles this
          }, 300);
        });
      })
      .then(() => {
        return originalFn(...args);
      });
  });
}

You may then use this function like:

const addCompareSnapshotCommand = require("cypress-visual-regression/dist/command");
const beforeCompareSnapshots = require("./commands/beforeCompareSnapshots");
addCompareSnapshotCommand({
  errorThreshold: 0.1
});
// add a before hook to compareSnapshot (this must be called AFTER compareSnapshotCommand() so the command can be overriden)
beforeCompareSnapshots(
  ".chromatic-ignore,[data-chromatic='ignore']",
  "._app-content"
);

In this example, we ignore the elements that are also ignored by 3rd party tool Chromatic.

Debug

set visualRegressionLogger to true to enable logging.