Coder Social home page Coder Social logo

sm1t / jest-puppeteer-react Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hapag-lloyd/jest-puppeteer-react

0.0 1.0 0.0 758 KB

Screenshot tests for your react components in chromium using puppeteer & jest

License: MIT License

JavaScript 94.40% Shell 1.52% HTML 1.94% Dockerfile 2.14%

jest-puppeteer-react's Introduction

jest-puppeteer-react Build Status

yarn add jest-puppeteer-react

This lib combines jest-puppeteer with webpack and webpack-dev-server to render your React components so you don't have to setup a server and navigate to it. It also includes jest-image-snapshot which adds the toMatchImageSnapshot matcher to expect.

Setup

  1. Use the preset in your jest configuration:

    {
      "preset": "jest-puppeteer-react"
    }
    

    Or require / include the needed scripts.

  2. Add a config file which contains a function to return a webpack config which is used to render:

    const webpack = require('webpack');
    const path = require('path');
    const buildDevWebpackConfig = require('./packages/core/dev/webpack/dev');
    
    module.exports = {
        generateWebpackConfig: function generateWebpackConfig(entryFiles, aliasObject) {
            const webpackConfig = buildDevWebpackConfig('test', {
                root: __dirname,
                app: 'x',
            }, {
                template: path.join(__dirname, './packages/dev-test-lib/screenshot/index.ejs'),
            }, webpack);
    
            webpackConfig.entry = { test: entryFiles };
            webpackConfig.resolve.alias = aliasObject;
    
            return webpackConfig;
        },
        port: 1111,
    };
    

Usage

Then use the specified render in your tests:

import React from 'react';
import { render } from 'jest-puppeteer-react';
import Button from '../Button';

describe('Button', () => {
    test('should render a button', async () => {
        await render(
            <Button>Button</Button>,
            { viewport: { width: 100, height: 100 } }
        );

        const screenshot = await page.screenshot();
        expect(screenshot).toMatchImageSnapshot();
    });
});

Options

The second argument of render takes some options to make things easier. You can also supply a default for this via the config.

{
    timeout: 60000, // 60 seconds
    viewport: {
        width: 100,
        height: 100,
        deviceScaleFactor: 2 // Retina Resolution
    }
}

Viewport

Automatically calls page.setViewport() for you. See puppeteer docs for options.

Configuration

You can put a jest-puppeteer-react.config.js file in your project root which gets automatically detected by jest-puppeteer-react.

Example:

const webpack = require('webpack');
const path = require('path');
const buildDevWebpackConfig = require('./packages/core/dev/webpack/dev');

module.exports = {
    generateWebpackConfig: function generateWebpackConfig(entryFiles, aliasObject) {
        const webpackConfig = buildDevWebpackConfig('test', {
            root: __dirname,
            app: 'x',
        }, {
            template: path.join(__dirname, './packages/dev-test-lib/screenshot/index.ejs'),
        }, webpack);

        webpackConfig.entry = { test: entryFiles };
        webpackConfig.resolve.alias = aliasObject;

        return webpackConfig;
    },
    port: 1111,
    renderOptions: {
        viewport: { deviceScaleFactor: 2 },
        dumpConsole: false, // set to true to dump console.* from puppeteer

        // function calls before page.goto()
        before: (page) => {
            // for example, disable cache
            await page.setCacheEnabled(false);
        },

        // function calls after page.goto()
        after: (page) => {},
    },
};

Configure Puppeteer

You can put a jest-puppeteer.config.js file in your root to configure puppeteer. This is a feature of the jest-puppeteer lib. See their readme for documentation: jest-puppeteer.

Configure ESLint

If you want to use the page object directly (without using the return value of render), you can set it as a global for eslint. See jest-puppeteer for an example.

Limitations

To be able to render the components in the browser, the test cases are required via webpack and the structure functions such as describe and test are evaluated. However, special behavior implemented in jest may be missing. For example mocks and timers are not supported currently. Furthermore at the moment hooks are not supported aswell. But they could be implemented quite easily.

jest-puppeteer-react's People

Contributors

albinotonnina avatar ansgarm avatar cruessler avatar doochik avatar kaiserkoenig avatar lodmfjord avatar onchainguy-eth avatar rosczja avatar schubidu avatar

Watchers

 avatar

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.