npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utrobin-jest-puppeteer-react

v7.0.3-v3

Published

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

Downloads

5

Readme

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.