Skip to content

Commit a6a9ceb

Browse files
committed
chore: add code coverage
1 parent ead0cf8 commit a6a9ceb

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

.github/workflows/nodejs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ jobs:
124124
- name: Run E2E tests for webpack version ${{ matrix.webpack-version }}
125125
run: npm run test:e2e -- --shard=${{ matrix.shard }}
126126

127+
- name: generate coverage
128+
run: npx nyc report --reporter=lcov
129+
127130
- name: Submit coverage data to codecov
128131
uses: codecov/codecov-action@v4
129132
with:

test/helpers/playwright-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
"use strict";
22

3+
const fs= require('fs');
4+
const path= require('path');
5+
const crypto= require('crypto');
36
const { test } = require("@playwright/test");
47

8+
const istanbulCLIOutput = path.join(process.cwd(), '.nyc_output');
9+
10+
function generateUUID() {
11+
return crypto.randomBytes(16).toString('hex');
12+
}
13+
514
const customTest = test.extend({
615
done: [
716
// eslint-disable-next-line no-empty-pattern
@@ -17,6 +26,25 @@ const customTest = test.extend({
1726
},
1827
{ option: true },
1928
],
29+
context: async ({ context }, use) => {
30+
await context.addInitScript(() =>
31+
// eslint-disable-next-line no-undef
32+
window.addEventListener('beforeunload', () =>
33+
// eslint-disable-next-line no-undef
34+
window.collectIstanbulCoverage(JSON.stringify(window.__coverage__))
35+
),
36+
)
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);}
41+
});
42+
await use(context);
43+
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__)))
46+
}
47+
}
2048
});
2149

2250
module.exports = { test: customTest };

0 commit comments

Comments
 (0)