Skip to content

Commit 90983fb

Browse files
committed
feat: take screenshot when a test fails
Fix #131
1 parent 559b271 commit 90983fb

File tree

3 files changed

+135
-11
lines changed

3 files changed

+135
-11
lines changed

package-lock.json

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/jest-environment-puppeteer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"chalk": "^4.1.2",
4646
"cwd": "^0.10.0",
47+
"filenamify": "^4.3.0",
4748
"jest-dev-server": "^7.0.0",
4849
"jest-environment-node": "^29.4.1",
4950
"merge-deep": "^3.0.3"

packages/jest-environment-puppeteer/src/env.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// eslint-disable-next-line
2-
import { join } from "node:path";
2+
import { join, basename } from "node:path";
33
import NodeEnvironment from "jest-environment-node";
44
import chalk from "chalk";
55
import { mkdir } from "node:fs/promises";
66
import { readConfig, getPuppeteer } from "./readConfig";
7+
import filenamify from "filenamify";
78

89
const handleError = (error) => {
910
process.emit("uncaughtException", error);
@@ -20,9 +21,38 @@ const getWorkerIndex = () => process.env.JEST_WORKER_ID - 1;
2021
const getEndpointIndex = () =>
2122
Math.min(+process.env.BROWSERS_COUNT - 1, getWorkerIndex());
2223

23-
// const screenshotsDirectory = join(process.cwd(), "screenshots");
24+
const screenshotsDirectory = join(process.cwd(), "screenshots");
25+
26+
const screenshot = async (page, name) => {
27+
await mkdir(screenshotsDirectory, { recursive: true });
28+
const filename = filenamify(`${name} (failed).jpg`);
29+
await page.screenshot({
30+
path: join(screenshotsDirectory, filename),
31+
});
32+
};
33+
34+
const getFullTestName = (test) => {
35+
const names = [];
36+
let current = test;
37+
while (current) {
38+
if (current.name === "ROOT_DESCRIBE_BLOCK") break;
39+
names.unshift(current.name);
40+
current = current.parent;
41+
}
42+
return names.join(" ");
43+
};
44+
45+
const getErrorScreenshotName = (test, filename) => {
46+
const fullName = getFullTestName(test);
47+
if (!filename) return fullName;
48+
return `${basename(filename)} - ${fullName}`;
49+
};
2450

2551
class PuppeteerEnvironment extends NodeEnvironment {
52+
constructor(config, context) {
53+
super(config, context);
54+
this.testPath = context.testPath;
55+
}
2656
// Jest is not available here, so we have to reverse engineer
2757
// the setTimeout function, see https://github.com/facebook/jest/blob/v23.1.0/packages/jest-runtime/src/index.js#L823
2858
setTimeout(timeout) {
@@ -34,14 +64,14 @@ class PuppeteerEnvironment extends NodeEnvironment {
3464
}
3565
}
3666

37-
// async handleTestEvent(event, state) {
38-
// if (event.name === "test_fn_failure") {
39-
// const testName = state.currentlyRunningTest.name;
40-
// await this.global.page.screenshot({
41-
// path: join(screenshotsDirectory, `${testName}.jpg`),
42-
// });
43-
// }
44-
// }
67+
async handleTestEvent(event, state) {
68+
if (event.name === "test_fn_failure") {
69+
await screenshot(
70+
this.global.page,
71+
getErrorScreenshotName(state.currentlyRunningTest, this.testPath)
72+
);
73+
}
74+
}
4575

4676
async setup() {
4777
const config = await readConfig();
@@ -169,7 +199,6 @@ class PuppeteerEnvironment extends NodeEnvironment {
169199
};
170200

171201
await this.global.jestPuppeteer.resetBrowser();
172-
await mkdir(join(process.cwd(), "screenshots"), { recursive: true });
173202
}
174203

175204
async teardown() {

0 commit comments

Comments
 (0)