@rws-air/jestscreenshot
TypeScript icon, indicating that this package has built-in type declarations

5.0.1 • Public • Published

logo

Rijkswaterstaat jestscreenshot

Creates screenshots with Puppeteer when a Jest test fails and optionally upload it to Slack


Project Status

GitHub

Bundle Sizes

npm bundle size npm bundle size

Versions

npm

Our Badges

website slack trello Pivotal Tracked Sketch Designed Zeplin Based


Installation

Install with yarn or npm:

yarn add -D @rws-air/jestscreenshot @types/jest-environment-puppeteer jest-circus jest-environment-puppeteer

Usage

const JestScreenshot = require('@rws-air/jestscreenshot');

About automated Slack uploading

This library supports uploading your screenshots to Slack by providing the SLACK_WEBTOKEN environment variable, or, through the slackToken option. You will also nede to provide the Slack Channel ID(s) through the option slackChannels and setting slackUpload option to true.

Example

Please note that you cannot use ES module imports in the jest-enviroment.ts file. Jest does not support them.

// jest.config.js

module.exports = {
  testRunner: 'jest-circus/runner',
  testEnvironment: '<rootDir>/__tests__/jest-environment.js',

  testMatch: ['<rootDir>/__tests__/index.test.ts'],

  globalSetup: 'jest-environment-puppeteer/setup',
  globalTeardown: 'jest-environment-puppeteer/teardown',
  setupFilesAfterEnv: ['expect-puppeteer', '<rootDir>/__tests__/jest-framework.ts']
};
// jest-framework.ts

jest.retryTimes(2);
// jest-environment.ts

/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/explicit-member-accessibility */
const PuppeteerEnvironment = require('jest-environment-puppeteer');
const JestScreenshot = require('@rws-air/jestscreenshot');
require('jest-circus');

const retryAttempts = process.env.RETRY_ATTEMPTS || 2;

// @ts-ignore
class CustomEnvironment extends PuppeteerEnvironment {
  async setup() {
    await super.setup();

    if (!process.env.CI) await this.localSetup();
  }

  async teardown() {
    // @ts-ignore
    await this.global.page.waitFor(2000);
    await super.teardown();
  }

  async handleTestEvent(event, state) {
    if (event.name === 'test_fn_failure') {
      if (state.currentlyRunningTest.invocations > retryAttempts) {
        const testName = state.currentlyRunningTest.name;
        const jestScreenshot = new JestScreenshot({
          page: this.global.page,
          dirName: __dirname,
          testName,
          slackChannels: ['ChannelID'],
          slackUpload: true
        });

        await jestScreenshot.setup();
      }
    }
  }
}

module.exports = CustomEnvironment;

API Documentation

Check out the docs on github pages

Dependents (0)

Package Sidebar

Install

npm i @rws-air/jestscreenshot

Weekly Downloads

9

Version

5.0.1

License

AGPL-3.0-or-later

Unpacked Size

66.9 kB

Total Files

8

Last publish

Collaborators

  • anouskadegraaf
  • favna
  • airbot
  • jeroenmies