All Projects → msn0 → vrt-react

msn0 / vrt-react

Licence: MIT License
Take a screenshot 📸 of React component. Push it and compare images in pull request.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to vrt-react

Lancia
网页转PDF渲染服务。提供收据、发票、报告或任何网页内容转PDF的微服务
Stars: ✭ 108 (+468.42%)
Mutual labels:  screenshot, puppeteer
nightwatch-vrt
Visual Regression Testing tools for nightwatch.js
Stars: ✭ 59 (+210.53%)
Mutual labels:  screenshot, visual-regression-testing
Site Scan
CLI for capturing website screenshots, powered by puppeteer.
Stars: ✭ 137 (+621.05%)
Mutual labels:  screenshot, puppeteer
Dark Mode Screenshot
This Puppeteer script takes a 📷 screenshot of a webpage in 🌞 Light and 🌒 Dark Mode.
Stars: ✭ 47 (+147.37%)
Mutual labels:  screenshot, puppeteer
jest-puppe-shots
A Jest plugin for creating screenshots of React components with a little help of Puppeteer
Stars: ✭ 86 (+352.63%)
Mutual labels:  screenshot, puppeteer
Page2image
📷 page2image is a npm package for taking screenshots which also provides CLI command
Stars: ✭ 66 (+247.37%)
Mutual labels:  screenshot, puppeteer
puppet-master
Puppeteer as a service hosted on Saasify.
Stars: ✭ 25 (+31.58%)
Mutual labels:  screenshot, puppeteer
Storycap
A Storybook Addon, Save the screenshot image of your stories 📷 via puppeteer.
Stars: ✭ 451 (+2273.68%)
Mutual labels:  screenshot, puppeteer
browser-automation-api
Browser automation API for repetitive web-based tasks, with a friendly user interface. You can use it to scrape content or do many other things like capture a screenshot, generate pdf, extract content or execute custom Puppeteer, Playwright functions.
Stars: ✭ 24 (+26.32%)
Mutual labels:  screenshot, puppeteer
autoscreen
Automated screen capture utility
Stars: ✭ 76 (+300%)
Mutual labels:  screenshot, screenshot-testing
Chart To Aws
Microservice to generate screenshot from a webpage and upload it to a AWS S3 Bucket.
Stars: ✭ 43 (+126.32%)
Mutual labels:  screenshot, puppeteer
screenie-server
A Node server with a pool of Puppeteer (Chrome headless) instances for scalable screenshot generation.
Stars: ✭ 19 (+0%)
Mutual labels:  screenshot, puppeteer
Jsdom Screenshot
📸 Take screenshots of jsdom with puppeteer
Stars: ✭ 39 (+105.26%)
Mutual labels:  screenshot, puppeteer
Puppeteer Dart
A Dart library to automate the Chrome browser over the DevTools Protocol. This is a port of the Puppeteer API
Stars: ✭ 92 (+384.21%)
Mutual labels:  screenshot, puppeteer
Chromda
λ 🖼️ Chromda is an AWS Lambda function for capturing screenshots of websites.
Stars: ✭ 481 (+2431.58%)
Mutual labels:  screenshot, puppeteer
Dhalang
Generate PDFs and make screenshots of HTML using Puppeteer in Ruby
Stars: ✭ 41 (+115.79%)
Mutual labels:  screenshot, puppeteer
Docker Puppeteer
docker image with Google Puppeteer installed
Stars: ✭ 415 (+2084.21%)
Mutual labels:  screenshot, puppeteer
Puppetron
Puppeteer (Headless Chrome Node API)-based rendering solution.
Stars: ✭ 429 (+2157.89%)
Mutual labels:  screenshot, puppeteer
mugshot
Framework independent visual testing library
Stars: ✭ 126 (+563.16%)
Mutual labels:  screenshot, puppeteer
screenshot
A screenshot API to convert web to image or PDF. Supports desktop and mobile views.
Stars: ✭ 108 (+468.42%)
Mutual labels:  screenshot, puppeteer

📸
Take screenshots of React components.
Push it and compare images in pull request.

Travis Status

compare screenshots

📸 @vrt/react

Motivation

Once we have unit tests of React components or any other type of tests it's also good to have screenshots of presentational components. Unit tests describe the logic of our component but they don't say anything about how it should look like. It's an anti-pattern to unit-test whether the component has received a proper className - the className can be valid but the color, font-size, margin or any other css property might be wrong causing our component to be badly coloured, displaced or just to look bad.

GitHub, GitLab and BitBucket do have image diff tools. Once someone created a pull request with code changes it's very helpful to see how the code changes affect the look of the component. Having component's screenshot included in the pull request gives the reviewer a chance to see what's really going on with the component's look.

Installation

$ npm i --global @vrt/react

Usage

  $ vrt --help

    Usage
      $ vrt [<file|glob> ...]

    Options
      --fail    don't update snapshots, fail if they don't match
      --watch   keep the server running, watching and recompiling your files
      --config  path to config file
      --dev     make webpack create dev bundles

    Examples
      $ vrt
      $ vrt src/button/
      $ vrt src/button/.vrt.js
      $ vrt --watch
      $ vrt --config config/vrt.config.js --watch src/**/.vrt.js
      $ npx vrt --watch --dev -- src/components/messages

Running @vrt/react in --watch mode will create sandboxed environment for each of the components, e.g.

$ vrt --watch

[email protected] is running with pattern: /Users/foo.bar/work/badges/!(node_modules)/**/.vrt.js

badge
  badge: http://localhost:56211/badge_badge.html

badge-with-props
  no message: http://localhost:56211/badge-with-props_no-message.html
  one message: http://localhost:56211/badge-with-props_one-message.html
  some messages: http://localhost:56211/badge-with-props_some-messages.html

button
  primary: http://localhost:56211/button_primary.html
  secondary: http://localhost:56211/button_secondary.html
  success: http://localhost:56211/button_success.html
  danger: http://localhost:56211/button_danger.html
  badge: http://localhost:56211/button_badge.html
  no type: http://localhost:56211/button_no-type.html

Basic example

Let's assume most simple case - we have a very simple badge.js component which doesn't take any props and doesn't have dependencies (except React), e.g.

import React from 'react';

export default function Badge () {
    return (
        <div>
            Lorem ipsum dolor sit amet
        </div>
    );
}

To capture a screenshot of it we need to create .vrt.js with a content:

module.exports = {
    main: 'badge.js'
}

This .vrt.js file informs @vrt/react that it should generate one screenshot for badge.js file. We save .vrt.js next to our component, e.g.

src/components/badge
├── badge.js
└── .vrt.js

Once we have it it's time to generate our first screenshot. Just run vrt, e.g.

$ npx vrt

Component with props

Component's without props might be a rare case. Most of the time components do receive props, e.g.

import React from 'react';

export default function Badge ({ num = 0 }) {
    return (
        <div>
            { num > 0 ? num : 'no' } new messages
        </div>
    );
}

There are two possible cases in here - the one with num greater then zero (e.g. „7 new messages”) and the other one with num not being greater than zero („no new messages”). To take a screenshots of these two cases we need to extend .vrt.js configuration and add possible props in the following way:

module.exports = {
    main: 'badge.js',
    presets: [
        {
            name: 'some messages',
            props: {
                messages: 7
            }
        },
        {
            name: 'no messages',
            props: {
                messages: 0
            }
        }
    ]
}

Run npx vrt. Two screenshots of our Badge component has been saved under badge/__screenshots__ directory:

src/components/badge/__screenshots__
├── no-messages-snap.png
└── some-messages-snap.png

Component with CSS modules

Let's say we have badge.css stylesheet and we import it inside our component, e.g.

import React from 'react';
import styles from './badge.css';

export default function Badge ({ num = 0 }) {
    return (
        <div className={ styles.badge }>
            { num > 0 ? num : 'no' } new messages
        </div>
    );
}

When @vrt/react takes a screenshot of your component it bundles it with webpack under the hood. To make it „understand” what import styles from './badge.css means we need to provide an appropriate loader, in this case it's possibly css-loader and styles-loader. We need to create a separate, global config for @vrt/react and feed it with loaders

// vrt.config.js
module.exports = {
    webpack: {
        loaders: [
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: { modules: true }
                    }
                ]
            }
        ]
    }
}

We save this file under our project's root directory and give it the name vrt.config.js. If you don't use webpack and don't have these loaders as dependencies you need to install them.

Next we are ready to run npx vrt.

Components with named imports

If your component exposes named import, e.g.

import React from 'react';

export function MyNamedcomponent () {
    // ...
}

then namedImport option should be used, e.g.

module.exports = {
    main: 'my-named-component',
    presets: [
        {
            name: 'simplest case',
            namedImport: 'MyNamedComponent',
            // ...
        }
    ]
}

Custom webpack loaders

If your component requires any other loaders to make it working - just add them to vrt.config.js. Please note that you don't need to add babel-loader - it's already added to make @vrt/react understand ES6 syntax.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].