• Stars
    star
    874
  • Rank 50,242 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Catch visual regressions in Cypress

Cypress Image Snapshot

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.

Discord

See it in action!

Cypress GUI

When using cypress open, errors are displayed in the GUI.

Cypress Image Snapshot in action

Composite Image Diff

When an image diff fails, a composite image is constructed.

Cypress Image Snapshot diff

Test Reporter

When using cypress run and --reporter cypress-image-snapshot/reporter, diffs are output to your terminal.

Cypress Image Snapshot reporter

Installation

Install from npm

npm install --save-dev cypress-image-snapshot

then add the following in your project's <rootDir>/cypress/plugins/index.js:

const {
  addMatchImageSnapshotPlugin,
} = require('cypress-image-snapshot/plugin');

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

and in <rootDir>/cypress/support/commands.js add:

import { addMatchImageSnapshotCommand } from 'cypress-image-snapshot/command';

addMatchImageSnapshotCommand();

Syntax

// addMatchImageSnapshotPlugin
addMatchImageSnapshotPlugin(on, config);

// addMatchImageSnapshotCommand
addMatchImageSnapshotCommand();
addMatchImageSnapshotCommand(commandName);
addMatchImageSnapshotCommand(options);
addMatchImageSnapshotCommand(commandName, options);

// matchImageSnapshot
.matchImageSnapshot();
.matchImageSnapshot(name);
.matchImageSnapshot(options);
.matchImageSnapshot(name, options);

// ---or---

cy.matchImageSnapshot();
cy.matchImageSnapshot(name);
cy.matchImageSnapshot(options);
cy.matchImageSnapshot(name, options);

Usage

In your tests

describe('Login', () => {
  it('should be publicly accessible', () => {
    cy.visit('/login');

    // snapshot name will be the test title
    cy.matchImageSnapshot();

    // snapshot name will be the name passed in
    cy.matchImageSnapshot('login');

    // options object passed in
    cy.matchImageSnapshot(options);

    // match element snapshot
    cy.get('#login').matchImageSnapshot();
  });
});

Updating snapshots

Run Cypress with --env updateSnapshots=true in order to update the base image files for all of your tests.

Preventing failures

Run Cypress with --env failOnSnapshotDiff=false in order to prevent test failures when an image diff does not pass.

Reporter

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 😎.

Multiple reporters

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:

{
  "reporterEnabled": "spec, mocha-junit-reporter, cypress-image-snapshot/reporter",
  "mochaJunitReporterReporterOptions": {
    "mochaFile": "cypress/results/results-[hash].xml"
  }
}

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"
  }
}

Options

  • 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:

addMatchImageSnapshotCommand({
  failureThreshold: 0.03, // threshold for entire image
  failureThresholdType: 'percent', // percent of image or number of pixels
  customDiffConfig: { threshold: 0.1 }, // threshold for each pixel
  capture: 'viewport', // capture viewport in screenshot
});

How it works

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:

  1. Take a screenshot with cy.screenshot() named according to the current test.
  2. Check if a saved snapshot exists in <rootDir>/cypress/snapshots and if so diff against that snapshot.
  3. If there is a resulting diff, save it to <rootDir>/cypress/snapshots/__diff_output__.

Cypress Version Requirements

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.

More Repositories

1

formik

Build forms in React, without the tears 😭
TypeScript
33,550
star
2

tsdx

Zero-config CLI for TypeScript package development
JavaScript
11,163
star
3

razzle

✨ Create server-rendered universal JavaScript applications with no configuration
JavaScript
11,089
star
4

backpack

πŸŽ’ Backpack is a minimalistic build system for Node.js projects.
JavaScript
4,470
star
5

the-platform

Web. Components. πŸ˜‚
TypeScript
4,406
star
6

after.js

Next.js-like framework for server-rendered React apps built with React Router
TypeScript
4,131
star
7

react-fns

Browser API's turned into declarative React components and HoC's
TypeScript
3,739
star
8

awesome-react-render-props

Awesome list of React components with render props
1,366
star
9

presspack

πŸ’» Wordpress like it's 2022 with Webpack and Docker
JavaScript
689
star
10

react-parcel-example

Minimum viable React app with Parcel Bundler
JavaScript
485
star
11

minimum-viable-saas

A multi-tier membership SaaS in less than 500 lines of code w/Stripe and Firebase
JavaScript
442
star
12

formik-persist

πŸ’Ύ Persist and rehydrate a Formik form to localStorage
TypeScript
375
star
13

mutik

A tiny (495B) immutable state management library based on Immer
TypeScript
325
star
14

typescript

TypeScript coding guidelines & configs for Formik
JavaScript
284
star
15

react-conf-2018

React Conf 2018 Source Code for "Moving to Suspense" Demo
JavaScript
226
star
16

tsdx-monorepo

A really good starting point for your next React x TypeScript monorepo
TypeScript
176
star
17

react-email-workflow

Newsletter design tool
JavaScript
175
star
18

formik-effect

Declarative component for managing side-effects in Formik forms. 580 bytes
TypeScript
166
star
19

formover

Build forms that pop bottles 🍾with Formik and React Popper
TypeScript
160
star
20

react-simple-infinite-scroll

A brutally simple react infinite scroll component
TypeScript
141
star
21

react-persist

πŸ’Ύ Persist and rehydrate React state to localStorage.
JavaScript
124
star
22

nextra-blank-custom-theme

A forkable Next.js site w/ a blank custom Nextra theme (w/Tailwind)
JavaScript
108
star
23

hyperhue

🌈 A fun HyperTerm theme that responds to your Philips Hue lights
JavaScript
103
star
24

reason-react-native-web-example

Razzle + Reason-React + React-Native-Web. Damn that's a lot of R's.
Reason
100
star
25

disco.chat

Add real-time ephemeral chat to any webpage.
TypeScript
93
star
26

react-router-nextjs-like-data-fetching

Demonstrating React Router 4's SSR awesomeness
JavaScript
89
star
27

dotfiles

My setup
Shell
50
star
28

razzle-react-vue-elm-php-lol

πŸ”₯ Blazing fast Razzle app with React, Vue, PHP, and Elm + HMR
JavaScript
50
star
29

formik-alicante

Formik slides & demos from React Alicante
JavaScript
46
star
30

react-suspense-playground

Stock Market News app w/ React Suspense
JavaScript
43
star
31

razzle-unrouted

Blazingly fast server-rendered MVC Webapps with Preact and Webpack
JavaScript
42
star
32

nextjs-langchain-example

Demo of using LangChain.js with Next.js and Vercel Edge Functions (to stream the response)
TypeScript
42
star
33

codemods

Collection of codemods for TypeScript and JavaScript codebases
JavaScript
41
star
34

framer-electron-preview

Quickly run Framer prototypes within Electron.
JavaScript
35
star
35

country-fns

🌏 Useful country data for forms and stuff.
JavaScript
34
star
36

TIL

πŸ“–Trying to document some of my learnings
30
star
37

squeezy

1 kB React component for accessible accordions / collapse UI
TypeScript
30
star
38

react-europe-2019

Slides and demo app from my keynote
JavaScript
29
star
39

jpjs

Some TypeScript utils
TypeScript
21
star
40

emotion-jsxstyle

jsxstyle primitives powered by emotion
JavaScript
20
star
41

react-router-suspense-demo

React Suspense x React Router Exploration
JavaScript
17
star
42

nextjs-route-handler-email

Experimenting with react.email and Next.js 13 Route Handlers
TypeScript
16
star
43

react-snippets

My React snippets for JavaScript and TypeScript
13
star
44

jaredpalmer.github.io

TypeScript
12
star
45

framer-router

A little routing solution for Framer.js
CoffeeScript
11
star
46

thinkaboutthis.fm

Source code for thinkaboutthis.fm
TypeScript
11
star
47

formik-bloomberg-talk

Slides and examples from my talk at Bloomberg on October 31, 2019
JavaScript
10
star
48

jaredpalmer-vscode-extensionpack

All of my VS Code extensions .... in one extension pack
9
star
49

electron-starter

A minimal Electron starter
JavaScript
8
star
50

babel-preset-react-ts

Create React App's Babel 7 Preset, plus TypeScript
JavaScript
7
star
51

noirny

Website for Noir New York (formerly the lately). Opening 11/21/19
TypeScript
7
star
52

jest-jsxstyle

πŸƒ Jest utilities for JSXStyle
JavaScript
6
star
53

flask-vercel

Python
6
star
54

jaredpalmer

it me.
5
star
55

saas-subdomain-nginx-node-docker

Example setup of SaaS-like user subdomains using Express, NGINX, and Docker Compose
JavaScript
5
star
56

juice.now.sh

Automattic's Juice CSS inliner as a microservice
HTML
4
star
57

datocms-next-js-blog-demo-9687

JavaScript
4
star
58

react-error-overlay-razzle-bug

JavaScript
3
star
59

glamor-jsxstyle

Future home of glamor-jsxstyle
JavaScript
3
star
60

reactnyc-formik

πŸ“ˆ Formik presentation at the August React NYC meetup @Spotify
JavaScript
3
star
61

bf-solid-addons

Helpful addons to Buzzfeed's Solid CSS Framework
CSS
2
star
62

codesandbox-template-next.js

Next.js template for CodeSandbox Projects
TypeScript
2
star
63

framer-electron

Prototype desktop apps with Framer.js and Electron with your own editor.
JavaScript
2
star
64

formik-docs

WIP. Formik docs website
TypeScript
2
star
65

oss-deck

JavaScript
1
star
66

downshift-razzle-bug

JavaScript
1
star
67

next-back-button-error

JavaScript
1
star
68

flask-pipenv-example

Python
1
star
69

os-today

A list a of the GitHub repos you should be using.
JavaScript
1
star
70

blocks

TypeScript
1
star
71

ci-info

Go
1
star
72

hyperneon

Hyperterm theme that rotates neon colors
JavaScript
1
star