Skip to content

Commit b5c8008

Browse files
gustavoliraclaude
andcommitted
feat: add auto-fixture for Istanbul coverage collection
Add a `_coverageCollector` auto-fixture that collects `window.__coverage__` from the browser after each test and writes it to a JSON file. This enables E2E coverage collection for instrumented dynamic plugins without modifying any test files. Guarded by E2E_COLLECT_COVERAGE=1 — zero overhead when disabled. Coverage files are written to coverage/istanbul/ (or COVERAGE_OUTPUT_DIR) and consumed by the overlay repo's coverage reporter for merging into lcov and uploading to Codecov. Part of the E2E coverage pipeline: RHIDP-13411 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a23767c commit b5c8008

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/playwright/fixtures/test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { test as base } from "@playwright/test";
33
import { LoginHelper, UIhelper } from "../helpers/index.js";
44
import { runOnce } from "../run-once.js";
55
import { $ } from "../../utils/bash.js";
6+
import fs from "node:fs";
67
import path from "path";
78

89
type RHDHDeploymentTestFixtures = {
910
rhdh: RHDHDeployment;
1011
uiHelper: UIhelper;
1112
loginHelper: LoginHelper;
13+
_coverageCollector: void;
1214
};
1315

1416
type RHDHDeploymentWorkerFixtures = {
@@ -61,6 +63,35 @@ const baseTest = base.extend<
6163
},
6264
{ scope: "test" },
6365
] as const,
66+
_coverageCollector: [
67+
async ({ page }, use, testInfo) => {
68+
await use();
69+
if (process.env.E2E_COLLECT_COVERAGE !== "1") return;
70+
try {
71+
const coverage = await page.evaluate(
72+
() =>
73+
(
74+
globalThis as unknown as {
75+
__coverage__?: Record<string, unknown>;
76+
}
77+
).__coverage__,
78+
);
79+
if (!coverage) return;
80+
const dir = path.resolve(
81+
process.cwd(),
82+
process.env.COVERAGE_OUTPUT_DIR || "coverage/istanbul",
83+
);
84+
fs.mkdirSync(dir, { recursive: true });
85+
fs.writeFileSync(
86+
path.join(dir, `${testInfo.testId}-${Date.now()}.json`),
87+
JSON.stringify(coverage),
88+
);
89+
} catch {
90+
// Best-effort: page may have crashed or been closed
91+
}
92+
},
93+
{ auto: true, scope: "test" },
94+
],
6495
});
6596

6697
export const test = Object.assign(baseTest, {

0 commit comments

Comments
 (0)