-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcoverage.ts
More file actions
48 lines (37 loc) · 1.21 KB
/
Copy pathcoverage.ts
File metadata and controls
48 lines (37 loc) · 1.21 KB
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
44
45
46
47
48
import { test as base, expect } from '@playwright/test'
import * as fs from 'fs/promises'
import * as path from 'path'
const coverageDir = path.resolve(process.cwd(), '.nyc_output')
interface CoverageWindow extends Window {
__coverage__?: Record<string, unknown>
}
const sanitize = (value: string) => {
return value.replace(/[^a-zA-Z0-9_-]+/g, '_')
}
const test = base.extend<{ _collectCoverage: void }>({
_collectCoverage: [
async ({ page }, use, testInfo) => {
await use()
if (process.env.COVERAGE !== '1') {
return
}
try {
const coverage = await page.evaluate(
() => (window as unknown as CoverageWindow).__coverage__ ?? null,
)
if (!coverage) {
return
}
await fs.mkdir(coverageDir, { recursive: true })
const title = sanitize(testInfo.titlePath.join('-'))
const fileName = `${testInfo.project.name}-${title}.json`
const filePath = path.join(coverageDir, fileName)
await fs.writeFile(filePath, JSON.stringify(coverage), 'utf8')
} catch {
// Ignore coverage collection failures to avoid masking test results.
}
},
{ auto: true },
],
})
export { expect, test }