-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcoverage.js
37 lines (31 loc) · 941 Bytes
/
coverage.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
/* global process */
import { test } from '@playwright/test';
import { randomUUID } from 'crypto';
import { promises as fs } from 'fs';
import * as path from 'path';
// See also https://playwright.dev/docs/api/class-coverage
if (process.env.NODE_V8_COVERAGE) {
test.beforeEach(async ({ page }) => {
await page.coverage.startJSCoverage();
});
test.afterEach(async ({ page }) => {
const coverage = await page.coverage.stopJSCoverage();
const output = {
result: coverage.map((entry) => ({
...entry,
url: resolveFileUrl(entry.url),
})),
};
await fs.mkdir(process.env.NODE_V8_COVERAGE, { recursive: true });
await fs.writeFile(
path.join(process.env.NODE_V8_COVERAGE, `coverage-${randomUUID()}.json`),
JSON.stringify(output),
);
});
}
function resolveFileUrl(url) {
return url.replace(
'http://localhost:8080',
`file://${path.resolve('public')}`,
);
}