Rijkswaterstaat jestscreenshot
Creates screenshots with Puppeteer when a Jest test fails and optionally upload it to Slack
Project Status
![GitHub](https://img.shields.io/github/license/RWS-NL/air-node-packages?logo=github&style=flat-square)
Bundle Sizes
![npm bundle size](https://img.shields.io/bundlephobia/minzip/@rws-air/jestscreenshot?label=jestscreenshot%20-%20minzipped&logo=webpack&style=flat-square)
Versions
![npm](https://img.shields.io/npm/v/@rws-air/jestscreenshot?color=crimson&label=jestscreenshot%20version&logo=npm&style=flat-square)
Our Badges
![Zeplin Based](https://img.shields.io/badge/Zeplin-Based-blue.svg?longCache=true&style=flat-square&link=https://app.zeplin.io/project/587661de593c722607342d7b&colorA=000000&colorB=FEBD12&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAOCAYAAAAi2ky3AAAAAXNSR0IArs4c6QAAAjBJREFUKBWtUU1rE1EUPfe9yUyajza2TRsq2EVswQqK6N6FiOJGBbeuq+hfKLhw48qFVBRd6A8QxEVFEFy3qLhQUREqBa1JbU0ymXzMzLvXN60fsRay8cJ9vDfvnDPnnQv0qcal/HRtNnu8Dwy0E8C/kBsT0JTtSJPpZMeHPobpetk9mXKgKEZKfNpX+9TLdXoPv/a50pGNZvX5Rv4EODI0h3Iw40IfBLNCrIBQQ16OvINnrtH+2r2Et6MjWS4UzGu+pffgDFfJU3kPiFwgTgHSQyEBVGuOjlau/uNI3hYmUecFtUvNyNcBoG3JejuMmdvcMBVJR6t01hr6W0iWiiV0whfoZEYoysD4EUgnxm0pEQnZN1WjOHCyAlWQjoFUgifJ9eavwrt7D4Hj89wIZpU3mEFkXdjSwykxK61uHKFrvtOgTcL2ljtuxYiXGw2tzI0E68j96d3dbvQYgnHHswJWRIiF69yJ1+ByTaUpI2lybMhJGUFcaSXtW865wdut1eTzZnLhnfJh1uZBasyMx1W4EumfLJttO4Z9FIjswNbbMLat2Iqj6XTuZvAqEUnq9wjkWflp/Ll7LFy2uXg2GEvmiAE/BFsx7horhi9a6LrjqEfpef/DlsTW2jMO8ZwJt6WH9UJ7qVE1lbDILClFUPat7zWpBWiUssXRh8G3talekWT/R0hwBTSwqE69aW4HNS9mD7CiTt4dWvQr65PIqM1ctuP6noPLoxOJWF/g/wD8AHyJ+Mm/jBDkAAAAAElFTkSuQmCC)
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.
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.retryTimes(2);
const PuppeteerEnvironment = require('jest-environment-puppeteer');
const JestScreenshot = require('@rws-air/jestscreenshot');
require('jest-circus');
const retryAttempts = process.env.RETRY_ATTEMPTS || 2;
class CustomEnvironment extends PuppeteerEnvironment {
async setup() {
await super.setup();
if (!process.env.CI) await this.localSetup();
}
async teardown() {
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