1
1
"use strict" ;
2
2
3
- const fs = require ( 'fs' ) ;
4
- const path = require ( ' path' ) ;
5
- const crypto = require ( ' crypto' ) ;
3
+ const fs = require ( "fs" ) ;
4
+ const path = require ( " path" ) ;
5
+ const crypto = require ( " crypto" ) ;
6
6
const { test } = require ( "@playwright/test" ) ;
7
7
8
- const istanbulCLIOutput = path . join ( process . cwd ( ) , ' .nyc_output' ) ;
8
+ const istanbulCLIOutput = path . join ( process . cwd ( ) , " .nyc_output" ) ;
9
9
10
10
function generateUUID ( ) {
11
- return crypto . randomBytes ( 16 ) . toString ( ' hex' ) ;
11
+ return crypto . randomBytes ( 16 ) . toString ( " hex" ) ;
12
12
}
13
13
14
14
const customTest = test . extend ( {
@@ -29,22 +29,32 @@ const customTest = test.extend({
29
29
context : async ( { context } , use ) => {
30
30
await context . addInitScript ( ( ) =>
31
31
// eslint-disable-next-line no-undef
32
- window . addEventListener ( ' beforeunload' , ( ) =>
32
+ window . addEventListener ( " beforeunload" , ( ) =>
33
33
// eslint-disable-next-line no-undef
34
- window . collectIstanbulCoverage ( JSON . stringify ( window . __coverage__ ) )
35
- ) ,
36
- )
34
+ window . collectIstanbulCoverage ( JSON . stringify ( window . __coverage__ ) ) ,
35
+ ) ,
36
+ ) ;
37
37
await fs . promises . mkdir ( istanbulCLIOutput , { recursive : true } ) ;
38
- await context . exposeFunction ( 'collectIstanbulCoverage' , ( coverageJSON ) => {
39
- if ( coverageJSON )
40
- { fs . writeFileSync ( path . join ( istanbulCLIOutput , `playwright_coverage_${ generateUUID ( ) } .json` ) , coverageJSON ) ; }
38
+ await context . exposeFunction ( "collectIstanbulCoverage" , ( coverageJSON ) => {
39
+ if ( coverageJSON ) {
40
+ fs . writeFileSync (
41
+ path . join (
42
+ istanbulCLIOutput ,
43
+ `playwright_coverage_${ generateUUID ( ) } .json` ,
44
+ ) ,
45
+ coverageJSON ,
46
+ ) ;
47
+ }
41
48
} ) ;
42
49
await use ( context ) ;
43
50
for ( const page of context . pages ( ) ) {
44
- // eslint-disable-next-line no-await-in-loop,no-undef
45
- await page . evaluate ( ( ) => window . collectIstanbulCoverage ( JSON . stringify ( window . __coverage__ ) ) )
51
+ // eslint-disable-next-line no-await-in-loop
52
+ await page . evaluate ( ( ) =>
53
+ // eslint-disable-next-line no-undef
54
+ window . collectIstanbulCoverage ( JSON . stringify ( window . __coverage__ ) ) ,
55
+ ) ;
46
56
}
47
- }
57
+ } ,
48
58
} ) ;
49
59
50
60
module . exports = { test : customTest } ;
0 commit comments