• Stars
    star
    618
  • Rank 72,605 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 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

A dead simple library to screenshot test React components

Warning

This package is no longer actively maintained.

Check out @previewjs/screenshot for an alternative.

React Screenshot Test

Logo

CircleCI npm downloads license



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 technique Supported
<div style={...} βœ…
CSS stylesheets
import "./style.css"
βœ…
Sass stylesheets
import "./style.scss"
βœ…
CSS Modules
import 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

Tool Visual Open Source Price for 100,000 snapshots/month Jest integration Review 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 πŸ™ƒ

More Repositories

1

previewjs

Preview UI components in your IDE instantly
TypeScript
1,831
star
2

viteshot

Viteshot πŸ“Έ is a fast and simple component screenshot tool based on Vite.
TypeScript
214
star
3

prmonitor

A browser extension to keep track of incoming and outgoing PRs
TypeScript
117
star
4

codetree

CodeTree is an ES6 library (written in TypeScript) that helps transform code easily.
TypeScript
69
star
5

bazel-javascript

JavaScript and TypeScript rules for Bazel (React friendly)
Python
65
star
6

deploy

A step-by-step, open-source tool for easy Docker deployments to AWS
TypeScript
43
star
7

electron-vue-typescript-starter

Starter project for an Electron app with Vue in TypeScript.
37
star
8

js-deps

Interactive graph of dependencies between JS/TS files
TypeScript
37
star
9

webdriverio-jest

Example of a WebdriverIO test written with the Jest framework
JavaScript
21
star
10

vite-gatsby-sample-app

CSS
15
star
11

ts-shift

Generate efficient, TypeScript-friendly value validators from TypeScript types.
TypeScript
15
star
12

previewjs-solid-plugin

TypeScript
14
star
13

sample-antlr4-typescript

A sample TypeScript app that parses a grammar with ANTLR4.
TypeScript
12
star
14

apigen

A simple DSL to generate a valid Swagger, OpenAPI or JSON Schema spec in Ruby
Ruby
7
star
15

rollup-plugin-friendly-type-imports

TypeScript
4
star
16

sample-js-selfparsing

A JS script that parses its own code and rewrites itself using the TypeScript library
JavaScript
4
star
17

namefinder

A domain name suggestion engine
TypeScript
3
star
18

hungry

A sample app to demonstrate Viteshot and React Preview
TypeScript
2
star
19

bazel-typescript-example

Python
2
star
20

comet

A dashboard that helps you compare branches and tags for any GitHub repository
TypeScript
2
star
21

ferns

A lightweight tree visualisation library
TypeScript
2
star
22

android-docker-app

A sample Android app built with Gradle inside a Docker container.
Java
2
star
23

talk-browser-extension

Talk about building a browser extension with React
JavaScript
2
star
24

talk-browser-extension-demo

Demo code for https://fwouts.github.io/talk-browser-extension
TypeScript
1
star
25

pdf-downloader

TypeScript
1
star
26

xml

An XML parser in TypeScript. Mostly just for fun.
TypeScript
1
star
27

instant

A minimal prototype of an instant code editor. It runs JS code as you type.
JavaScript
1
star
28

goto

A simplistic short links server. Just playing around with Node.js and Mongoose.
JavaScript
1
star
29

rentals

A technical demo built with TypeScript, React, MobX, Express and PostgreSQL via TypeORM.
TypeScript
1
star
30

jit

A command-line tool to simplify Git workflows for Jira users
Go
1
star
31

js-manualtexteditor

Just for fun (proof of concept): a text editor implemented without contenteditable
JavaScript
1
star
32

sample-aurelia-places-autocomplete

Sample: Autocomplete places with Aurelia.io
JavaScript
1
star