@bahmutov/cypress-toy-visual-testing

中文文档
1.8.6发布于 2025/10/15MITTypeScript近 30 天下载 1986

A simple local visual testing for Cypress users

原始使用文档
文档目录6 节
  1. Learn
  2. Install
  3. Use
  4. Capture options
  5. CI options
  6. Development

@bahmutov/cypress-toy-visual-testing main

A simple local visual testing for Cypress users

Learn

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. If the diff is below this percentage, the gold image won't be overwritten
  • failOnLayoutDiff fails the image comparison if the dimensions differ, true by default
  • dimensionTolerance allow each dimension to be different by this ratio if failOnLayoutDiff: true

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

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

Compare the images, allow each dimension to be within 5% of the gold image

cy.imageDiff('app', {
  failOnLayoutDiff: true,
  dimensionTolerance: 0.05,
})

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

Development

Use npm run watch to compile TS into JS

Use the following icons to refer to images:

  • 🖼️ gold image
  • 📸 current screenshot
  • ✅ image match
  • 🔥 image mismatch

版本与发布记录

@bahmutov/cypress-toy-visual-testing 共发布 33 个版本,首次发布于 2024/05/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.8.6latest2025/10/151.1 年前
1.8.52025/10/151.1 年前
1.8.42025/10/151.1 年前
1.8.32025/05/091.5 年前
1.8.22025/05/091.5 年前
1.8.12025/03/101.7 年前

关于 @bahmutov/cypress-toy-visual-testing 的常见问题

@bahmutov/cypress-toy-visual-testing 是什么?

A simple local visual testing for Cypress users

如何安装 @bahmutov/cypress-toy-visual-testing?

在项目目录运行 npm install @bahmutov/cypress-toy-visual-testing,或使用 pnpm add / yarn add / bun add。

@bahmutov/cypress-toy-visual-testing 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bahmutov/cypress-toy-visual-testing 可以商用吗?

@bahmutov/cypress-toy-visual-testing 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bahmutov/cypress-toy-visual-testing 的下载量有多少?

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

@bahmutov/cypress-toy-visual-testing 还在维护吗?

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