1
1
// eslint-disable-next-line
2
- import { join } from "node:path" ;
2
+ import { join , basename } from "node:path" ;
3
3
import NodeEnvironment from "jest-environment-node" ;
4
4
import chalk from "chalk" ;
5
5
import { mkdir } from "node:fs/promises" ;
6
6
import { readConfig , getPuppeteer } from "./readConfig" ;
7
+ import filenamify from "filenamify" ;
7
8
8
9
const handleError = ( error ) => {
9
10
process . emit ( "uncaughtException" , error ) ;
@@ -20,9 +21,38 @@ const getWorkerIndex = () => process.env.JEST_WORKER_ID - 1;
20
21
const getEndpointIndex = ( ) =>
21
22
Math . min ( + process . env . BROWSERS_COUNT - 1 , getWorkerIndex ( ) ) ;
22
23
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
+ } ;
24
50
25
51
class PuppeteerEnvironment extends NodeEnvironment {
52
+ constructor ( config , context ) {
53
+ super ( config , context ) ;
54
+ this . testPath = context . testPath ;
55
+ }
26
56
// Jest is not available here, so we have to reverse engineer
27
57
// the setTimeout function, see https://github.com/facebook/jest/blob/v23.1.0/packages/jest-runtime/src/index.js#L823
28
58
setTimeout ( timeout ) {
@@ -34,14 +64,14 @@ class PuppeteerEnvironment extends NodeEnvironment {
34
64
}
35
65
}
36
66
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
+ }
45
75
46
76
async setup ( ) {
47
77
const config = await readConfig ( ) ;
@@ -169,7 +199,6 @@ class PuppeteerEnvironment extends NodeEnvironment {
169
199
} ;
170
200
171
201
await this . global . jestPuppeteer . resetBrowser ( ) ;
172
- await mkdir ( join ( process . cwd ( ) , "screenshots" ) , { recursive : true } ) ;
173
202
}
174
203
175
204
async teardown ( ) {
0 commit comments