@emerson-eps/cypress-image-snapshot

中文文档
1.5.3发布于 2024/10/30近 30 天下载 3882

Cypress Image Snapshot binds jest-image-snapshot's image diffing logic to Cypress.io commands.

原始使用文档
文档目录12 节
  1. Installation
  2. TypeScript
  3. Usage
  4. In your tests
  5. Updating snapshots
  6. Requiring snapshots to be present
  7. How it works
  8. Requirements
  9. Contributing
  10. Setup
  11. Working on the plugin
  12. Forked from @simonsmith/cypress-image-snapshot

Cypress Image Snapshot

Cypress Image Snapshot binds jest-image-snapshot's image diffing logic to Cypress.io commands.

build-and-test

Installation

Install with your chosen package manager

# yarn
yarn add --dev @emerson-eps/cypress-image-snapshot

# npm
npm install --save-dev @emerson-eps/cypress-image-snapshot

Next, import the plugin function and add it to the setupNodeEvents function:

// cypress.config.ts

import {defineConfig} from 'cypress'
import {addMatchImageSnapshotPlugin} from '@emerson-eps/cypress-image-snapshot/plugin'

export default defineConfig({
  e2e: {
    setupNodeEvents(on) {
      addMatchImageSnapshotPlugin(on)
    },
  },
})

Add the command to your relevant support file:

// cypress/support/e2e.ts

import {addMatchImageSnapshotCommand} from '@emerson-eps/cypress-image-snapshot'

addMatchImageSnapshotCommand()

// can also add any default options to be used
// by all instances of `matchImageSnapshot`
addMatchImageSnapshotCommand({
  failureThreshold: 0.2,
})

TypeScript

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": {
    // ...
  },
  "include": ["@emerson-eps/cypress-image-snapshot"]
}

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({
      failureThreshold: 0.4
      blur: 10
      timeout: 3000
    });

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

Options

The options object combines jest-image-snapshot and Cypress screenshot configuration.

cy.matchImageSnapshot({
  // options for jest-image-snapshot
  failureThreshold: 0.2,
  comparisonMethod: 'ssim',

  // options for Cypress.screenshot()
  capture: 'viewport',
  blackout: ['.some-element'],
})

New options have been implemented to make the function recursive and to retry the snapshots:

cy.matchImageSnapshot({
  // Time it takes for the snapshot command to time out if the snapshot is not correct
  timeout: 5000,

  //Sets a delay between recursive snapshots
  delayBetweenTries: 1000,
})

Updating snapshots

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

Requiring snapshots to be present

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.

How it works

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

Requirements

Tested on Cypress 10.x, 11.x and 12.x

Cypress must be installed as a peer dependency

Contributing

Setup

  • Clone the repository and install the yarn dependencies with yarn install
  • Ensure that Docker is setup. This is necessary for generating/updating snapshots
  • Using Volta is recommended for managing Node and Yarn versions. These are automatically picked up from the package.json
  • Commits should be based on conventional-changelog

Working on the plugin

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:

open

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

run

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

Note on environment variables

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

Forked from @simonsmith/cypress-image-snapshot

This is a rewrite of the plugin from Simon Smith.

版本与发布记录

@emerson-eps/cypress-image-snapshot 共发布 12 个版本,首次发布于 2023/06/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.3latest2024/10/302 年前
1.5.22023/07/293 年前
1.5.12023/07/283 年前
1.5.02023/07/283 年前
1.4.02023/07/263 年前
1.3.12023/07/113 年前

关于 @emerson-eps/cypress-image-snapshot 的常见问题

@emerson-eps/cypress-image-snapshot 是什么?

Cypress Image Snapshot binds jest-image-snapshot's image diffing logic to Cypress.io commands.

如何安装 @emerson-eps/cypress-image-snapshot?

在项目目录运行 npm install @emerson-eps/cypress-image-snapshot,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:cypress(>10.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@emerson-eps/cypress-image-snapshot 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/emerson-eps__cypress-image-snapshot 是否存在。

@emerson-eps/cypress-image-snapshot 可以商用吗?

@emerson-eps/cypress-image-snapshot 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@emerson-eps/cypress-image-snapshot 的下载量有多少?

根据 npm 官方下载统计,@emerson-eps/cypress-image-snapshot 在 2026/10/14 – 2026/11/12 这 30 天内被下载 3,882 次,日均约 129 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@emerson-eps/cypress-image-snapshot 还在维护吗?

最新版本 1.5.3 发布于 2024/10/30(2 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/06/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。