Git Product home page Git Product logo

storybook-wdio's Introduction

Storybook Visual Regression boilerplate

Storybook Visual Regression boilerplate

Storybook boilerplate which combines ReactJS, Typescript and Visual Regression testing using WebDriverIO

Build Status CircleCI GithubCI


Motivation

As we all know ReactJS continues to lead the way as far as being the most widely used Javascript library and in the opinion of many, the most powerful. If you combine ReactJS with Storybook and visual regression testing from Webdriver IO it is possible to create a design system and a custom component library that can be safely reused between products and/or other teams. Sometimes when sharing a UI library among a large group or multiple teams we run in to issues when one person makes what might seem to be trivial or small change to a shared component. These changes might cause issues for other users who are using the same component in another place. Often times small modifications or changes can slip through code reviews too. With this boilerplate we incorporated the ability to run visual regression testing on each component. This allows developers to see even the slightest of changes and decide if it is acceptible or not before commiting to the changes.

Getting started

To run this project please follow these steps:

  1. Make sure you have Docker & NodeJS installed on your machine
  2. Pull the standalone-chrome-debug or standalone-firefox-debug docker image
  3. Clone this repo
  4. Install dependencies
npm install
  1. Start storybook with development mode
npm run storybook

How to create a React Component along with a Story

Structure

my-react-component
  |-- stories/
      |-- vr-test/
          |-- index.spec.ts
      |-- story-1.story.tsx
      |-- story-2.story.tsx
      |-- story-3.story.tsx
  |-- index.tsx
  |-- style.less

Component template

src/components/button/index.tsx

import * as React from 'react';
import * as style from './style.less';

export class Button extends React.Component<Props> {
  public render(): JSX.Element {
    const className = [style.container, style[this.props.size || '']];

    return (
      <button onClick={this.props.onClick} id="button" className={className.join(' ')}>
        {this.props.children}
      </button>
    );
  }
}

export type Props = DataProps & EventProps;

interface DataProps {
  /** Children node */
  children: string | React.ReactNode;
  /** Size of button */
  size?: 'small' | 'medium' | 'large';
}

interface EventProps {
  /** Click event */
  onClick?: (e: React.MouseEvent<HTMLElement>) => void;
}

src/components/button/style.less

.container {
  background: blue;
  width: 100%;
}

.small {
  background: yellow;
}

.medium {
  background: green;
}

.large {
  background: red;
}

Creating a Story

File name pattern: src/components/<component-name>/stories/<test-case-name>.story.tsx

Example:

src/components/button/stories/large.story.tsx

import { Props } from '..'; // import the Props from the component

export const test: Props = {
  children: 'large size',
  size: 'large',
};

src/components/button/stories/small.story.tsx

import { Props } from '..'; // import the Props from the component

export const test: Props = {
  children: 'small size',
  size: 'small',
};

Visual Regression Testing

This type of testing produces snapshots of the component as *.png files.

For example:

Here is a visual regression test for button component

button with large size

button large

button with medium size

button medium

button with small size

button small

Here is a visual regression test for text component

text with black background

button medium

text with red background

button small

After completing the React component, to run the visual regression test, you need to do a little set up.

Run selenium Docker image

Make sure to start Docker

To run Selenium of web drivers, you can choose either running Docker commands

docker run -d -p 4444:4444 -p 5900:5900 selenium/standalone-chrome-debug

or using docker-compose.yml

docker-compose up

Advance: You can customize export ports by arguments if default ports are already allocated

Port Default Description
CHROME_MAIN_PORT 4444 port of selenium chrome
CHROME_DEBUG_PORT 5900 port of selenium chrome debug - for screen sharing
FIREFOX_MAIN_PORT 5555 port of selenium firefox
FIREFOX_DEBUG_PORT 5901 port of selenium firefox debug - for screen sharing

Example

CHROME_MAIN_PORT=6666 CHROME_DEBUG_PORT=5909 docker-compose up

Create entry test file

Create the file src/components/<component-name>/stories/vr-test/index.spec.ts with code below

import { VisualRegressionTest } from 'lib/test/visual-regression-test';
import * as style from '../../style.less';

new VisualRegressionTest(__dirname, style.container).run();

style.container is the className wrapped around the component

Run the visual regression test

To run the visual regression test, make sure your storybook started.

Desktop

npm run test:chrome src/components/<component-name>/stories/vr-test/index.spec.ts

Smartphone

npm run test:chrome:smartphone src/components/<component-name>/stories/vr-test/index.spec.ts

Both Desktop and Smartphone

npm test src/components/<component-name>/stories/vr-test/index.spec.ts

Run all tests

npm test

Debug visual regression test

Mac

Screen Sharing

To debug visual regression testing

  1. Open the Screen Sharing
chrome

Hostname: `YOUR_MACHINE_IP`:5900
Password: secret
  1. Run test
  2. Navigate to Screen Sharing to see the step by step for running the test

Contributors โœจ


Dzung Nguyen

๐Ÿ“– ๐Ÿ’ป ๐Ÿค” ๐Ÿ‘€ ๐Ÿ›

Hoc Nguyen

๐Ÿ’ป ๐Ÿค” ๐Ÿ‘€ ๐Ÿ›

Khoa Do

๐Ÿ’ป ๐Ÿ‘€ ๐Ÿ›

Nguyen Van Hao

๐Ÿ’ป ๐Ÿ“–

Ben Lee

๐Ÿ“– ๐Ÿ’ป

storybook-wdio's People

Contributors

3benlee avatar allcontributors[bot] avatar davidnguyen11 avatar haoict avatar nvhoc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nvhoc dendadon

storybook-wdio's Issues

Restructure the tests

It does not make sense to create the __test contains the storybook config. So thinking another name such as stories and move the test file to another directory or single file

Add travis CI/CD

The CI/CD pipeline contains 3 steps:

  1. Run tslint
  2. Run vr-testing
  3. Build storybook

If there one of those steps failed, the CI/CD stop immediately.

Add support testing element's states

Proposal

Each element has the its own states like:

  • normal
  • hover & un-hover (back to normal state)
  • focus & blur (back to normal state)

Each state has its own style. In development, we rarely pay attention to these states, but it plays the important part in product development & design system. So that it has to be consistent. So that, to make sure it's consistent, testing is needed and we only focus on 3 main states:

  • normal
  • hover
  • focus

Because in visual regression testing, we only need to test how it looks in the UI in each state (component) not the whole logic and output of each state is a snapshot.

Re-write document

  • Introduction
  • Motivation
  • Getting started
  • Docker
  • Creating the components & stories
  • Running the tests

Add tslint

To make code convention consistently, It would be better to have tslint config to prevent developer mistake.

  1. Add tslint config file
  2. Add validate command in package.json

Add LICENSE file

MIT License

Copyright (c) 2019 Nguyen Ngoc Dzung

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Set up build tool

  • Set up build tool for building and export the component to the lib
  • User can install, import and use component and also can publish to the npm package

Update RegEx of test files and component files

https://github.com/davidnguyen179/storybook-wdio/blob/de654ef9a991f2cbe9da4b4c74e08bbeca88e752/.storybook/webpack.config.js#L56

https://github.com/davidnguyen179/storybook-wdio/blob/de654ef9a991f2cbe9da4b4c74e08bbeca88e752/.storybook/webpack.config.js#L57

is there anyway that we can have the pattern that support like this

src/components/button/index.tsx

It needs to match the components/**/

src/components/button/tests/data/large.spec.tsx

It needs to match the components/**/tests/data

How to tests?

  1. Open the storybook => Did components render correctly?
  2. Run the tests => Did it run tests successfully?

Related files

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.