@bahmutov/cypress-toy-visual-testing
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

@bahmutov/cypress-toy-visual-testing main

A simple local visual testing for Cypress users

Install

Add this plugin as a dev dependency

npm i -D @bahmutov/cypress-toy-visual-testing

Set it up from your `` hook in the Cypress config file:

// cypress.config.js

const { defineConfig } = require('cypress')
// https://github.com/bahmutov/cypress-toy-visual-testing
const {
  setupVisualTesting,
} = require('@bahmutov/cypress-toy-visual-testing/dist/plugin/setup')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      setupVisualTesting(on, config)
      return config
    },
  },
})

Add custom commands to your support file:

// cypress/support/e2e.js

// https://github.com/bahmutov/cypress-toy-visual-testing
import '@bahmutov/cypress-toy-visual-testing/dist/support/commands'

Use

Take full page screenshots

cy.imageDiff('added-todos')

Capture options

  • fullPage (default) takes screenshots of the entire page and stitches them into a single image
  • viewport takes the screenshot of the currently visible portion
  • clipToViewport takes the screenshot of the entire test runner and the clips the image to the viewport.
  • diffPercentage lets you ignore image differences for up to N percent of pixels
  • failOnLayoutDiff fails the image comparison if the dimensions differ, true by default

For example, let's ignore all image differences for up to half a percentage of pixels

cy.imageDiff('app', { diffPercentage: 0.5 })

CI options

The following Cypress env variables can change how the image diffs are approved or rejected:

  • updateGoldImages overwrites the gold images with the new screenshots, even if there are differences. Useful for branches in the pull request to overwrite the images which can be then added to the source branch. Then the user can see the difference between the changed gold images and the main branch's gold images.

  • failOnMissingGoldImage, default false can be used to automatically fail the cy.imageDiff if the screenshot does not have a gold image to compare itself to. For example, on the main branch, we could require all gold images to be present.

- name: Run Cypress tests 🧪
  # https://github.com/cypress-io/github-action
  uses: cypress-io/github-action@v6
  with:
    start: npm start
    # if there is no gold image to compare a screenshot to
    # it is a problem, and we should fail the tests
    env: failOnMissingGoldImage=true

Package Sidebar

Install

npm i @bahmutov/cypress-toy-visual-testing

Weekly Downloads

2,992

Version

1.5.0

License

MIT

Unpacked Size

66.6 kB

Total Files

9

Last publish

Collaborators

  • bahmutov