GithubHelp home page GithubHelp logo

corygibbons / component-image Goto Github PK

View Code? Open in Web Editor NEW
40.0 3.0 5.0 269 KB

๐Ÿ“ท Generate image snapshots of React components for visual regression testing

JavaScript 87.87% HTML 10.16% CSS 1.97%

component-image's Introduction

Component Image

Generate image snapshots of React components for visual regression testing. This package will only give you the image, you'll have to diff it with something else (like jest-image-snapshot).

npm version codeship code style: prettier

Usage

Install

npm install component-image

Use

import { generateImage } from 'component-image';

generateImage(component, options);

Default options

options = {
  // Path to .css file
  stylesheet: undefined;
  // Change size of screenshot
  viewport: {
    width: 800,
    height: 600,
  },
  renderer: ReactDOMServer.renderToStaticMarkup,
  image: {
    // Path to save image, likely unnecessary
    path: undefined,
  },
  // Options that are passed to Puppeteer
  puppeteerOptions: {}
}

Integration Example

Usage with jest-image-snapshot:

import React from 'react';
import { generateImage } from 'component-image'

const component = (
  <div>
    <h1>The Component</h1>
  </div>
);

describe('Test Component', () => {
  it('has no visual regressions', () => {

    return generateImage(component, {
      stylesheet: '../../style.css',
      viewport: {
        width: 1000,
        height: 860
      }
    }).then(image => {
      expect(image).toMatchImageSnapshot();
    });

  };
};

component-image's People

Contributors

apaatsio avatar corygibbons avatar penx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

component-image's Issues

Set delay before taking screenshot?

I'm working with a legacy component (that is itself a legacy D3 chart shoved into a React component and hacked together using refs). I'm unable to use component-image on it because the initial element is added to the DOM in the first run of render, but is not actually populated with a graph until componentDidMount runs. Therefore, I'm getting a blank screenshot.

Before I just bite the bullet and rewrite this crap (referring to my component), is there any way to set a small delay?

Fonts imported with @import are not loaded

I have fonts imported with CSS @import but the fonts are not in use in the generated image.

CSS:

@import url('https://fonts.googleapis.com/css?family=Merriweather');
h1 {
  font-family: 'Merriweather';
}

When I generate image with React component <h1>AaBbCc</h1> the imported font is not visible in the image.

Not able to render background image and fonts specified in css in screenshot

Hi @corygibbons,

I started using this lib for image comparision tests. My css has fonts and background images in it but while taking the screenshot both are missing. However normal static css is working fine. Can help with this?

my css looks like this:

.my_background_img {
background: url('placeholderimage.png') no-repeat;
}

this background is not getting applied in the html but all other css is getting applied.

Styled Components

Does this work with styled components? I tried providing the path to my styled component for stylesheet but the style doesn't get loaded.

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.