-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
43 lines (38 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const baseTest = require("@playwright/test").test;
const istanbulTempDir = process.env.ISTANBUL_TEMP_DIR
? path.resolve(process.env.ISTANBUL_TEMP_DIR)
: path.join(process.cwd(), ".nyc_output");
function generateUUID() {
return crypto.randomBytes(16).toString("hex");
}
const test = baseTest.extend({
context: async ({ context }, use) => {
await context.addInitScript(() =>
window.addEventListener("beforeunload", () =>
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__)),
),
);
await fs.promises.mkdir(istanbulTempDir, { recursive: true });
await context.exposeFunction("collectIstanbulCoverage", (coverageJSON) => {
if (coverageJSON)
fs.writeFileSync(
path.join(
istanbulTempDir,
`playwright_coverage_${generateUUID()}.json`,
),
coverageJSON,
);
});
await use(context);
for (const page of context.pages()) {
await page.evaluate(() =>
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__)),
);
}
},
});
const expect = test.expect;
module.exports = { test, expect };