You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
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.
Lorem ipsum dolor sit amet
);
}">importReactfrom'react';
exportdefaultfunctionBadge(){ 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.
{ num > 0 ? num : 'no' } new messages
);
}">importReactfrom'react';
exportdefaultfunctionBadge({ 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:
exportdefaultfunctionBadge({ num =0}){ return( <divclassName={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
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.
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
About
Take a screenshot of React component. Push it and compare images in pull request.