README
Rijkswaterstaat jestscreenshot
Creates screenshots with Puppeteer when a Jest test fails and optionally upload it to Slack
Project Status
Bundle Sizes
Versions
Our Badges
Installation
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