@gnuechtel/shared-coverage 0.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/package.json +14 -0
- package/src/index.d.ts +3 -0
- package/src/index.js +7 -0
- package/src/index.js.map +1 -0
- package/src/lib/create-istanbul-coverage-reports.d.ts +28 -0
- package/src/lib/create-istanbul-coverage-reports.js +58 -0
- package/src/lib/create-istanbul-coverage-reports.js.map +1 -0
- package/src/lib/merge-coverage.d.ts +1 -0
- package/src/lib/merge-coverage.js +114 -0
- package/src/lib/merge-coverage.js.map +1 -0
- package/src/lib/playwright-coverage.d.ts +4 -0
- package/src/lib/playwright-coverage.js +50 -0
- package/src/lib/playwright-coverage.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# shared-coverage
|
|
2
|
+
|
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
|
4
|
+
|
|
5
|
+
## Building
|
|
6
|
+
|
|
7
|
+
Run `nx build shared-coverage` to build the library.
|
|
8
|
+
|
|
9
|
+
## Running unit tests
|
|
10
|
+
|
|
11
|
+
Run `nx test shared-coverage` to execute the unit tests via [Jest](https://jestjs.io).
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gnuechtel/shared-coverage",
|
|
3
|
+
"version": "0.0.33",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"tslib": "^2.3.0",
|
|
6
|
+
"uuid": "^9.0.0"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"playwright": "^1.21.1"
|
|
10
|
+
},
|
|
11
|
+
"type": "commonjs",
|
|
12
|
+
"main": "./src/index.js",
|
|
13
|
+
"typings": "./src/index.d.ts"
|
|
14
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./lib/create-istanbul-coverage-reports"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./lib/merge-coverage"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./lib/playwright-coverage"), exports);
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/shared/coverage/src/index.ts"],"names":[],"mappings":";;;AAAA,iFAAuD;AACvD,+DAAqC;AACrC,oEAA0C"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates the final Istanbul coverage reports with nyc.
|
|
3
|
+
* Raw coverage must has been already collected in a `.nyc_output` directory.
|
|
4
|
+
*
|
|
5
|
+
* How is raw coverage collected? Either by a tool like a jest-playwright or by own code.
|
|
6
|
+
* Here is an example for playwright, how coverage may be collected in frontend application code:
|
|
7
|
+
*
|
|
8
|
+
* <code>
|
|
9
|
+
* // Create a client-side JavaScript function which stores coverage data in the file system
|
|
10
|
+
* await context.exposeFunction('collectIstanbulCoverage', (coverageJSON: string) => {
|
|
11
|
+
* // Create a unique coverage file for the current context
|
|
12
|
+
* fs.writeFileSync(path.join('.nyc_output', `playwright_coverage_${generateUUID()}.json`), coverageJSON);
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* // The init script is registered on the context, but used for every page
|
|
16
|
+
* // The code is executed before the page will is unloaded
|
|
17
|
+
* await context.addInitScript(() =>
|
|
18
|
+
* window.addEventListener('beforeunload', () =>
|
|
19
|
+
* // Pass coverage data of Istanbul to the exposed function
|
|
20
|
+
* (window as any).collectIstanbulCoverage(JSON.stringify((window as any).__coverage__))
|
|
21
|
+
* ),
|
|
22
|
+
* );
|
|
23
|
+
* </code>
|
|
24
|
+
*
|
|
25
|
+
* @param rootDirectory the directory where the `.nyc_output` must exist
|
|
26
|
+
* @param projectDirectory the directory where an additional `.nycrc.json` may exist
|
|
27
|
+
*/
|
|
28
|
+
export declare function createIstanbulCoverageReports(rootDirectory?: string, projectDirectory?: string): Promise<void>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createIstanbulCoverageReports = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const path = require("path");
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
8
|
+
const NYC = require('nyc');
|
|
9
|
+
/**
|
|
10
|
+
* Creates the final Istanbul coverage reports with nyc.
|
|
11
|
+
* Raw coverage must has been already collected in a `.nyc_output` directory.
|
|
12
|
+
*
|
|
13
|
+
* How is raw coverage collected? Either by a tool like a jest-playwright or by own code.
|
|
14
|
+
* Here is an example for playwright, how coverage may be collected in frontend application code:
|
|
15
|
+
*
|
|
16
|
+
* <code>
|
|
17
|
+
* // Create a client-side JavaScript function which stores coverage data in the file system
|
|
18
|
+
* await context.exposeFunction('collectIstanbulCoverage', (coverageJSON: string) => {
|
|
19
|
+
* // Create a unique coverage file for the current context
|
|
20
|
+
* fs.writeFileSync(path.join('.nyc_output', `playwright_coverage_${generateUUID()}.json`), coverageJSON);
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // The init script is registered on the context, but used for every page
|
|
24
|
+
* // The code is executed before the page will is unloaded
|
|
25
|
+
* await context.addInitScript(() =>
|
|
26
|
+
* window.addEventListener('beforeunload', () =>
|
|
27
|
+
* // Pass coverage data of Istanbul to the exposed function
|
|
28
|
+
* (window as any).collectIstanbulCoverage(JSON.stringify((window as any).__coverage__))
|
|
29
|
+
* ),
|
|
30
|
+
* );
|
|
31
|
+
* </code>
|
|
32
|
+
*
|
|
33
|
+
* @param rootDirectory the directory where the `.nyc_output` must exist
|
|
34
|
+
* @param projectDirectory the directory where an additional `.nycrc.json` may exist
|
|
35
|
+
*/
|
|
36
|
+
function createIstanbulCoverageReports(rootDirectory = '.', projectDirectory = '.') {
|
|
37
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
// Default options to process Istanbul JSON files
|
|
39
|
+
const defaultNycOptions = {
|
|
40
|
+
cwd: rootDirectory,
|
|
41
|
+
reportDir: './coverage',
|
|
42
|
+
reporter: ['lcov', 'clover', 'json', 'json-summary'],
|
|
43
|
+
extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
|
|
44
|
+
excludeAfterRemap: false,
|
|
45
|
+
};
|
|
46
|
+
// Combine default options with optional options from config file
|
|
47
|
+
const nycrcJsonFilename = path.join(projectDirectory, '.nycrc.json');
|
|
48
|
+
const nycrcJson = fs.existsSync(nycrcJsonFilename) ? JSON.parse(fs.readFileSync(nycrcJsonFilename, 'utf8')) : {};
|
|
49
|
+
const nycOptions = Object.assign({}, defaultNycOptions, nycrcJson);
|
|
50
|
+
console.log('Creating istanbul coverage reports: %o', nycOptions);
|
|
51
|
+
// Use nyc to convert Istanbul coverage JSON to different output formats
|
|
52
|
+
const nyc = new NYC(nycOptions);
|
|
53
|
+
yield nyc.report();
|
|
54
|
+
console.log('Coverage reports created');
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
exports.createIstanbulCoverageReports = createIstanbulCoverageReports;
|
|
58
|
+
//# sourceMappingURL=create-istanbul-coverage-reports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-istanbul-coverage-reports.js","sourceRoot":"","sources":["../../../../../../libs/shared/coverage/src/lib/create-istanbul-coverage-reports.ts"],"names":[],"mappings":";;;;AAAA,yBAAyB;AACzB,6BAA6B;AAE7B,8DAA8D;AAC9D,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,SAAsB,6BAA6B,CAAC,aAAa,GAAG,GAAG,EAAE,gBAAgB,GAAG,GAAG;;QAC7F,iDAAiD;QACjD,MAAM,iBAAiB,GAAG;YACxB,GAAG,EAAE,aAAa;YAClB,SAAS,EAAE,YAAY;YACvB,QAAQ,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,cAAc,CAAC;YACpD,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC;YACzD,iBAAiB,EAAE,KAAK;SACzB,CAAC;QAEF,iEAAiE;QACjE,MAAM,iBAAiB,GAAG,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,aAAa,CAAC,CAAC;QACrE,MAAM,SAAS,GAAG,EAAE,CAAC,UAAU,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACjH,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,iBAAiB,EAAE,SAAS,CAAC,CAAC;QAEnE,OAAO,CAAC,GAAG,CAAC,wCAAwC,EAAE,UAAU,CAAC,CAAC;QAClE,wEAAwE;QACxE,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,CAAC;QAChC,MAAM,GAAG,CAAC,MAAM,EAAE,CAAC;QACnB,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC;IAC1C,CAAC;CAAA;AApBD,sEAoBC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function mergeCoverage(): Promise<void>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergeCoverage = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const shared_nx_1 = require("@gnuechtel/shared-nx");
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
function mergeCoverage() {
|
|
9
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
10
|
+
const sourceDirs = yield detectSourceDirs();
|
|
11
|
+
const minSourceDirsLength = 2;
|
|
12
|
+
if (sourceDirs.length >= minSourceDirsLength) {
|
|
13
|
+
console.log('found coverage source dirs: %o', sourceDirs);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
// Not enough coverage files found
|
|
17
|
+
console.log(`Nothing to merge: ${sourceDirs.length} source dir(s) found, but ${minSourceDirsLength} required`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
// Create an empty merged and temp directory
|
|
21
|
+
prepareMergedDir();
|
|
22
|
+
// Copy coverage json sources
|
|
23
|
+
copyCoverageFiles(sourceDirs);
|
|
24
|
+
// merge
|
|
25
|
+
const mergeCommand = `nyc merge ${tempDir} "${path.join(tempDir, coverageJsonFileName)}"`;
|
|
26
|
+
yield (0, shared_nx_1.createProcess)(mergeCommand, '', process.cwd());
|
|
27
|
+
// report
|
|
28
|
+
const reportCommand = `nyc report -t ${tempDir} --report-dir ${mergedDir} --reporter=lcov --reporter=cobertura --reporter=text --reporter=clover --reporter=json`;
|
|
29
|
+
yield (0, shared_nx_1.createProcess)(reportCommand, '', process.cwd());
|
|
30
|
+
console.log(`merged report created in ${mergedDir}`);
|
|
31
|
+
// The temp directory is not required anymore
|
|
32
|
+
removeTempDir();
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
exports.mergeCoverage = mergeCoverage;
|
|
36
|
+
const coverageJsonFileName = 'coverage-final.json';
|
|
37
|
+
const rootDirectory = path.resolve(path.join(__dirname, '..', '..', '..', '..', '..'));
|
|
38
|
+
const coverageRoot = path.join(rootDirectory, 'dist', 'coverage');
|
|
39
|
+
const mergedDir = path.join(coverageRoot, 'merged');
|
|
40
|
+
const tempDir = path.join(mergedDir, 'temp');
|
|
41
|
+
function getFiles(dir) {
|
|
42
|
+
return tslib_1.__asyncGenerator(this, arguments, function* getFiles_1() {
|
|
43
|
+
const readdir = fs.promises.readdir;
|
|
44
|
+
const dirents = yield tslib_1.__await(readdir(dir, { withFileTypes: true }));
|
|
45
|
+
for (const dirent of dirents) {
|
|
46
|
+
const res = path.resolve(dir, dirent.name);
|
|
47
|
+
if (dirent.isDirectory()) {
|
|
48
|
+
yield tslib_1.__await(yield* tslib_1.__asyncDelegator(tslib_1.__asyncValues(getFiles(res))));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
yield yield tslib_1.__await(res);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function detectSourceDirs() {
|
|
57
|
+
var _a, e_1, _b, _c;
|
|
58
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
// Do not include target and temp directory in source dirs
|
|
60
|
+
const excludeDirs = [mergedDir, tempDir];
|
|
61
|
+
const sourceDirs = [];
|
|
62
|
+
try {
|
|
63
|
+
for (var _d = true, _e = tslib_1.__asyncValues(getFiles(coverageRoot)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
64
|
+
_c = _f.value;
|
|
65
|
+
_d = false;
|
|
66
|
+
const fullFileName = _c;
|
|
67
|
+
const baseName = path.basename(fullFileName);
|
|
68
|
+
const dirName = path.dirname(fullFileName);
|
|
69
|
+
if (baseName == coverageJsonFileName && !excludeDirs.includes(dirName)) {
|
|
70
|
+
sourceDirs.push(dirName);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
75
|
+
finally {
|
|
76
|
+
try {
|
|
77
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
78
|
+
}
|
|
79
|
+
finally { if (e_1) throw e_1.error; }
|
|
80
|
+
}
|
|
81
|
+
return sourceDirs;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function prepareMergedDir() {
|
|
85
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
if (!fs.existsSync(mergedDir)) {
|
|
87
|
+
fs.mkdirSync(mergedDir);
|
|
88
|
+
}
|
|
89
|
+
removeTempDir();
|
|
90
|
+
fs.mkdirSync(tempDir);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function copyCoverageFiles(sourceDirs) {
|
|
94
|
+
let i = 0;
|
|
95
|
+
for (const sourceDir of sourceDirs) {
|
|
96
|
+
if (fs.existsSync(sourceDir)) {
|
|
97
|
+
i++;
|
|
98
|
+
const source = path.join(sourceDir, coverageJsonFileName);
|
|
99
|
+
const target = path.join(tempDir, `${i}.json`);
|
|
100
|
+
// Complete coverage json file for merging
|
|
101
|
+
fs.copyFileSync(source, target);
|
|
102
|
+
console.log(`${path.basename(path.dirname(source))} coverage json file copied to ${path.basename(target)}`);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
console.warn(`coverage source dir ${sourceDir} does not exist`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function removeTempDir() {
|
|
110
|
+
if (fs.existsSync(tempDir)) {
|
|
111
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=merge-coverage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge-coverage.js","sourceRoot":"","sources":["../../../../../../libs/shared/coverage/src/lib/merge-coverage.ts"],"names":[],"mappings":";;;;AAAA,oDAAqD;AACrD,yBAAyB;AACzB,6BAA6B;AAE7B,SAAsB,aAAa;;QACjC,MAAM,UAAU,GAAG,MAAM,gBAAgB,EAAE,CAAC;QAE5C,MAAM,mBAAmB,GAAG,CAAC,CAAC;QAC9B,IAAI,UAAU,CAAC,MAAM,IAAI,m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EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;YAC7B,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;SACzB;QACD,aAAa,EAAE,CAAC;QAChB,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACxB,CAAC;CAAA;AAED,SAAS,iBAAiB,CAAC,UAAoB;IAC7C,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE;QAClC,IAAI,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;YAC5B,CAAC,EAAE,CAAC;YACJ,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,oBAAoB,CAAC,CAAC;YAC1D,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;YAC/C,0CAA0C;YAC1C,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YAChC,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,iCAAiC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SAC7G;aAAM;YACL,OAAO,CAAC,IAAI,CAAC,uBAAuB,SAAS,iBAAiB,CAAC,CAAC;SACjE;KACF;AACH,CAAC;AAED,SAAS,aAAa;IACpB,IAAI,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;QAC1B,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;KACtD;AACH,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { BrowserContext } from 'playwright';
|
|
2
|
+
export declare function prepareContextForIstanbulCoverage(context: BrowserContext): Promise<void>;
|
|
3
|
+
export declare function collectIstanbulCoverageForContext(context: BrowserContext): Promise<void>;
|
|
4
|
+
export declare function deleteExistingPlaywrightCoverageFiles(): void;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deleteExistingPlaywrightCoverageFiles = exports.collectIstanbulCoverageForContext = exports.prepareContextForIstanbulCoverage = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const rootDirectory = path.resolve(path.join(__dirname, '..', '..', '..', '..', '..'));
|
|
9
|
+
const coverageOutputDirectory = path.join(rootDirectory, '.nyc_output');
|
|
10
|
+
const playwrightCoverageFileNameStart = 'playwright_coverage_';
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
12
|
+
const debug = require('debug')('e2e:coverage');
|
|
13
|
+
function prepareContextForIstanbulCoverage(context) {
|
|
14
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
yield context.exposeFunction('collectIstanbulCoverage', (coverageJSON) => {
|
|
16
|
+
if (!fs.existsSync(coverageOutputDirectory)) {
|
|
17
|
+
fs.mkdirSync(coverageOutputDirectory);
|
|
18
|
+
}
|
|
19
|
+
if (!coverageJSON) {
|
|
20
|
+
debug('Browser coverage data is not available. Did you enable client-side coverage via window.__coverage__?');
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// Create a unique coverage file for the current context
|
|
24
|
+
fs.writeFileSync(path.join(coverageOutputDirectory, `${playwrightCoverageFileNameStart}${(0, uuid_1.v4)()}.json`), coverageJSON);
|
|
25
|
+
});
|
|
26
|
+
// The init script is registered on the context, but used for every page
|
|
27
|
+
// The code is executed before the page is unloaded
|
|
28
|
+
yield context.addInitScript(() => window.addEventListener('beforeunload', () =>
|
|
29
|
+
// Pass coverage data of Istanbul to the exposed function
|
|
30
|
+
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__))));
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
exports.prepareContextForIstanbulCoverage = prepareContextForIstanbulCoverage;
|
|
34
|
+
function collectIstanbulCoverageForContext(context) {
|
|
35
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
for (const page of context.pages()) {
|
|
37
|
+
yield page.evaluate(() => window.collectIstanbulCoverage(JSON.stringify(window.__coverage__)));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
exports.collectIstanbulCoverageForContext = collectIstanbulCoverageForContext;
|
|
42
|
+
function deleteExistingPlaywrightCoverageFiles() {
|
|
43
|
+
if (fs.existsSync(coverageOutputDirectory)) {
|
|
44
|
+
fs.readdirSync(coverageOutputDirectory)
|
|
45
|
+
.filter((f) => f.startsWith(playwrightCoverageFileNameStart))
|
|
46
|
+
.map((f) => fs.unlinkSync(path.join(coverageOutputDirectory, f)));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.deleteExistingPlaywrightCoverageFiles = deleteExistingPlaywrightCoverageFiles;
|
|
50
|
+
//# sourceMappingURL=playwright-coverage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"playwright-coverage.js","sourceRoot":"","sources":["../../../../../../libs/shared/coverage/src/lib/playwright-coverage.ts"],"names":[],"mappings":";;;;AACA,+BAAoC;AACpC,yBAAyB;AACzB,6BAA6B;AAE7B,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AACvF,MAAM,uBAAuB,GAAG,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AACxE,MAAM,+BAA+B,GAAG,sBAAsB,CAAC;AAE/D,8DAA8D;AAC9D,MAAM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