npm package: @mihkeleidast/jest-puppeteer-react
Description: screenshot tests for your react components in chromium using puppeteer & jest
Author: Ansgar Mertens
Latest version: 7.0.0-1

# @mihkeleidast/jest-puppeteer-react

```
npm install @mihkeleidast/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": "@mihkeleidast/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:

```ecmascript 6
import React from 'react';
import { render } from '@mihkeleidast/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](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport) 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 add a `jestPuppeteerConfig` key to `jest-puppeteer-react.config.js` to configure puppeteer. That key accepts the same values as `jest-puppeteer.config.js` would file in your root when configuring jest-puppeteer manually. This is a feature of the jest-puppeteer lib. See their readme for documentation: [jest-puppeteer](https://github.com/smooth-code/jest-puppeteer/tree/6cd3050e472c9a8bcdb18e2635a40ad674c4b795#configure-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](https://github.com/smooth-code/jest-puppeteer/tree/6cd3050e472c9a8bcdb18e2635a40ad674c4b795#configure-eslint) 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.