react-screenshot-test

fwoutsMIT长期未更新

给 React 组件做截图测试的极简库,基于 Jest 自动生成快照并比对视觉差异,帮你在改样式时及时发现 UI 回归。

A dead simple library to screenshot test React components

暂无版本616star22fork同步于 2026-11-07 07:33 UTC

怎么上手

由 README 提炼,执行前请确认
npm install react-screenshot-test
jest -c jest.screenshot.config.js -u
硬件要求按 README 说明

README 未说明具体硬件要求;默认需 Docker 运行 Chrome 截图,Linux 下用 host 网络模式占用 3001 端口,可设 SCREENSHOT_MODE=local 改用本地浏览器。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

给 React 组件做截图测试的 Jest 插件,用 Puppeteer 生成图片快照并比对差异

给 React 组件做截图测试的极简库,基于 Jest 自动生成快照并比对视觉差异,帮你在改样式时及时发现 UI 回归。

核心能力

  • 通过 Jest 配置生成组件截图快照
  • 本地起服务端渲染组件,Puppeteer 逐页截图
  • 默认在 Docker 内跑 Chrome 保证跨平台一致
  • 支持 CSS Modules、Sass、Emotion、Styled Components
  • 可接入 Percy 把快照存到第三方服务

容易踩的坑

项目已不再积极维护;默认依赖 Docker 跑 Chrome,未装 Docker 需设 SCREENSHOT_MODE=local;截图文件建议用 Git LFS 存储以免仓库膨胀。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Warning

    This package is no longer actively maintained.

    React Screenshot Test

    Logo

    Here is a screenshot test written with react-screenshot-test:

    Code example

    All you need is to install react-screenshot-test and configure Jest:

    // jest.screenshot.config.js
    
    module.exports = {
      testEnvironment: "node",
      globalSetup: "react-screenshot-test/global-setup",
      globalTeardown: "react-screenshot-test/global-teardown",
      testMatch: ["**/?(*.)+(screenshot).[jt]s?(x)"],
      transform: {
        "^.+\\.[t|j]sx?$": "babel-jest", // or ts-jest
        "^.+\\.module\\.css$": "react-screenshot-test/css-modules-transform",
        "^.+\\.css$": "react-screenshot-test/css-transform",
        "^.+\\.scss$": "react-screenshot-test/sass-transform",
        "^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
          "react-screenshot-test/asset-transform"
      },
      transformIgnorePatterns: ["node_modules/.+\\.js"]
    };
    

    You can then generate screenshots with jest -c jest.screenshot.config.js -u, just like you would with Jest snapshots.

    What does it look like?

    Here's a real example of a pull request where a component was changed: Example PR

    How does it work?

    Under the hood, we start a local server which renders components server-side. Each component is given its own dedicated page (e.g. /render/my-component). Then we use Puppeteer to take a screenshot of that page.

    Curious to learn more? Check out the internal documentation!

    Cross-platform consistency

    If you work on a team where developers use a different OS (e.g. Mac OS and Linux), or if you develop on Mac OS but use Linux for continuous integration, you would quickly run into issues where screenshots are inconsistent across platforms. This is, for better or worse, expected behaviour.

    In order to work around this issue, react-screenshot-test will default to running Puppeteer (i.e. Chrome) inside Docker to take screenshots of your components. This ensures that generated screenshots are consistent regardless of which platform you run your tests on.

    You can override this behaviour by setting the SCREENSHOT_MODE environment variable to local, which will always use a local browser instead of Docker.

    Note: On Linux, react-screenshot-test will run Docker using host network mode on port 3001

    CSS support

    CSS-in-JS libraries such as Emotion and Styled Components are supported.

    CSS techniqueSupported
    <div style={...}✅
    CSS stylesheets import "./style.css"✅
    Sass stylesheetsimport "./style.scss"✅
    CSS Modulesimport css from "./style.css"✅
    Emotion✅
    Styled Components✅

    Usage with create-react-app

    If you'd like to set up react-screenshot-test with a create-react-app, here is everything you need.

    Storing image snapshots

    We recommend using Git LFS to store image snapshots. This will help prevent your Git repository from becoming bloated over time.

    If you're unfamiliar with Git LFS, you can learn about it with this short video (2 min) and/or going through the official tutorial.

    To set up Git LFS, install the Git extension and add the following to .gitattributes in your repository (source):

    **/__screenshots__/*.* binary
    **/__screenshots__/*.* filter=lfs diff=lfs merge=lfs -text
    

    You may also need to set up Git LFS for continuous integration. See our config for an example with CircleCI.

    Usage with Percy

    If you prefer to keep image snapshots out of your repository, you can use a third-party service such as Percy:

    • Install @percy/puppeteer
    • Ensure that PERCY_TOKEN is set in your enviroment
    • Set up a script to invoke Jest through Percy:
    {
      "screenshot-test-percy": "SCREENSHOT_MODE=percy percy exec -- jest -c jest.screenshot.config.js"
    }
    

    TypeScript support

    This library is written in TypeScript. All declarations are included.

    Browser support

    At the moment, screenshots are only generated with Chrome. However, the design can be extended to any headless browser. File an issue if you'd like to help make this happen.

    Comparison

    ToolVisualOpen SourcePrice for 100,000 snapshots/monthJest integrationReview process
    react-screenshot-test✅✅Free✅Pull request
    Jest snapshots❌✅Free✅Pull request
    Percy✅❌$469❌Separate UI
    storycap✅✅Free❌Implicit approval with reg-suit

    Reporting issues

    If something doesn't work, or if the documentation is unclear, please do not hesitate to raise an issue!

    If it doesn't work for you, it probably doesn't work for others either 🙃