|
| 1 | +# E2E Code Coverage for the RHDH Repository |
| 2 | + |
| 3 | +Jira: [RHIDP-13243](https://issues.redhat.com/browse/RHIDP-13243) |
| 4 | +Epic: [RHIDP-13242](https://issues.redhat.com/browse/RHIDP-13242) |
| 5 | +Feature: [RHDHPLAN-851](https://issues.redhat.com/browse/RHDHPLAN-851) |
| 6 | + |
| 7 | +## What this is |
| 8 | + |
| 9 | +This document describes how the `e2e-tests/` Playwright suite collects JavaScript |
| 10 | +coverage from Chromium while exercising a deployed RHDH instance, and how that |
| 11 | +coverage flows to Codecov. |
| 12 | + |
| 13 | +Scope of this mechanism: |
| 14 | + |
| 15 | +- **What it measures**: frontend JS/TS in `packages/app/src/**` and related modules, |
| 16 | + exercised by the Playwright specs in `e2e-tests/playwright/e2e/`. |
| 17 | +- **What it does not measure**: backend-only code, code not loaded by the browser |
| 18 | + during the E2E run, plugins delivered as separate OCI images (tracked under |
| 19 | + RHIDP-11866 via CoverPort). |
| 20 | + |
| 21 | +## How it works |
| 22 | + |
| 23 | +1. **Opt-in via env var.** When `COLLECT_COVERAGE=true`, a Playwright fixture |
| 24 | + (`e2e-tests/playwright/support/coverage/test.ts`) extends the base `test` |
| 25 | + and wraps each run with `page.coverage.startJSCoverage()` / `stopJSCoverage()`. |
| 26 | + Raw V8 coverage is persisted per test as JSON under `coverage/e2e-raw/`. |
| 27 | +2. **Aggregation at run end.** A Playwright reporter |
| 28 | + (`e2e-tests/playwright/support/coverage/reporter.ts`) reads the raw files at |
| 29 | + `onEnd` and uses `monocart-coverage-reports` to convert V8 output to |
| 30 | + Istanbul format and emit a merged LCOV + HTML report under `coverage/e2e/`. |
| 31 | +3. **Codecov upload (CI, follow-up).** A CI step uploads `coverage/e2e/lcov.info` |
| 32 | + to Codecov with the flag `rhdh-e2e-frontend`. This step lands in a follow-up |
| 33 | + PR (see Known Limitations below). |
| 34 | + |
| 35 | +With `COLLECT_COVERAGE` unset (the default), the fixture and reporter are no-ops |
| 36 | +and the E2E suite runs identically to the pre-coverage configuration. |
| 37 | + |
| 38 | +## How to use it locally |
| 39 | + |
| 40 | +### Prerequisites |
| 41 | + |
| 42 | +- A deployed RHDH reachable via `BASE_URL` |
| 43 | +- `yarn install` completed in `e2e-tests/` |
| 44 | +- Node.js 24 |
| 45 | + |
| 46 | +### Run a spec with coverage enabled |
| 47 | + |
| 48 | +```bash |
| 49 | +cd e2e-tests |
| 50 | +COLLECT_COVERAGE=true yarn playwright test playwright/e2e/smoke-test.spec.ts |
| 51 | +``` |
| 52 | + |
| 53 | +Outputs: |
| 54 | + |
| 55 | +- `e2e-tests/coverage/e2e-raw/` — one JSON file per test run (raw V8 coverage) |
| 56 | +- `e2e-tests/coverage/e2e/` — merged report: `lcov.info`, `coverage-summary.json`, |
| 57 | + HTML dashboard at `index.html` |
| 58 | + |
| 59 | +### View the HTML report |
| 60 | + |
| 61 | +```bash |
| 62 | +open e2e-tests/coverage/e2e/index.html |
| 63 | +``` |
| 64 | + |
| 65 | +## Migrating a spec to capture coverage |
| 66 | + |
| 67 | +There are two patterns depending on how the spec manages its browser page. |
| 68 | + |
| 69 | +### Specs that use the built-in `{ page }` fixture (most specs) |
| 70 | + |
| 71 | +Opt in by importing the extended `test`/`expect` from the coverage helper |
| 72 | +instead of `@playwright/test`: |
| 73 | + |
| 74 | +```ts |
| 75 | +// Before |
| 76 | +import { test, expect } from "@playwright/test"; |
| 77 | + |
| 78 | +// After — uses the @support path alias (configured in tsconfig.json), |
| 79 | +// so the import is the same regardless of file depth: |
| 80 | +import { test, expect } from "@support/coverage/test"; |
| 81 | +``` |
| 82 | + |
| 83 | +The rest of the spec stays identical — `describe`, `beforeAll`, `expect`, |
| 84 | +locators, and fixtures behave exactly the same. |
| 85 | + |
| 86 | +### Specs that create their own context/page via `browser.newContext()` |
| 87 | + |
| 88 | +Several existing specs (for example `plugins/adoption-insights`, |
| 89 | +`plugins/scorecard`) manage their own `BrowserContext` and `Page` in |
| 90 | +`beforeAll` instead of using the default `{ page }` fixture. These specs |
| 91 | +bypass the auto-instrumented fixture above — they need to call the helpers |
| 92 | +explicitly: |
| 93 | + |
| 94 | +```ts |
| 95 | +import { test, expect, startCoverageForPage, stopCoverageForPage } from "@support/coverage/test"; |
| 96 | + |
| 97 | +test.describe("my feature", () => { |
| 98 | + let context: BrowserContext; |
| 99 | + let page: Page; |
| 100 | + |
| 101 | + test.beforeAll(async ({ browser }) => { |
| 102 | + context = await browser.newContext(); |
| 103 | + page = await context.newPage(); |
| 104 | + }); |
| 105 | + |
| 106 | + test("something", async ({}, testInfo) => { |
| 107 | + await startCoverageForPage(page); |
| 108 | + try { |
| 109 | + // test body ... |
| 110 | + } finally { |
| 111 | + await stopCoverageForPage(page, testInfo); |
| 112 | + } |
| 113 | + }); |
| 114 | +}); |
| 115 | +``` |
| 116 | + |
| 117 | +`startCoverageForPage` and `stopCoverageForPage` are safe to call |
| 118 | +unconditionally — they are no-ops when `COLLECT_COVERAGE` is unset, and any |
| 119 | +internal failure is logged rather than propagated so coverage collection |
| 120 | +cannot fail a test. |
| 121 | + |
| 122 | +### Specs that have not migrated |
| 123 | + |
| 124 | +Run normally and simply do not contribute to coverage data. Migration is |
| 125 | +phased by design — this PR lands the scaffolding; spec migration happens |
| 126 | +incrementally in follow-up PRs so each batch can be reviewed in isolation. |
| 127 | + |
| 128 | +## Environment variables |
| 129 | + |
| 130 | +| Variable | Default | Purpose | |
| 131 | +|---|---|---| |
| 132 | +| `COLLECT_COVERAGE` | (unset) | Set to `true` to enable coverage collection | |
| 133 | +| `COVERAGE_OUTPUT_DIR` | `<cwd>/coverage/e2e-raw` | Where per-test raw V8 coverage is written | |
| 134 | +| `COVERAGE_REPORT_DIR` | `<cwd>/coverage/e2e` | Where the merged Istanbul report is written | |
| 135 | +| `COVERAGE_GENERATE_TIMEOUT_MS` | `120000` (2 min) | Maximum time the reporter will wait for `monocart.CoverageReport.generate()` before aborting. Prevents a CI job from hanging if coverage aggregation stalls | |
| 136 | + |
| 137 | +Raw coverage file names include the spec title path, worker index, and retry |
| 138 | +number (for example `Smoke_test_basic_flow-w0-r0-1714003200000.json`) so |
| 139 | +parallel workers and retries never overwrite each other's output. |
| 140 | + |
| 141 | +## Known limitations |
| 142 | + |
| 143 | +- **Chromium only.** `page.coverage` is a CDP feature and is not available in |
| 144 | + Firefox or WebKit. Chromium is the default browser for this suite, so this |
| 145 | + is not a practical issue today. |
| 146 | +- **Source maps required for navigable reports.** The deployed RHDH must ship |
| 147 | + source maps (or an equivalent mapping) for the report to link back to original |
| 148 | + source. If source maps are missing, coverage still works but will point to |
| 149 | + minified bundles. Validating source-map availability in the deployed image is |
| 150 | + a task in RHIDP-13243. |
| 151 | +- **CI upload not wired yet.** This PR lands the collection + aggregation |
| 152 | + infrastructure. The Codecov upload step in the Playwright CI jobs (handle_ocp_pull, |
| 153 | + handle_ocp_nightly) is a follow-up task. |
| 154 | +- **Specs not yet migrated.** This PR does not modify any existing spec. A |
| 155 | + follow-up PR migrates specs to the extended `test` import so they start |
| 156 | + contributing coverage data. |
| 157 | + |
| 158 | +## Related coverage flags in Codecov |
| 159 | + |
| 160 | +| Flag | Source | Scope | |
| 161 | +|---|---|---| |
| 162 | +| `rhdh` | Jest (`packages/*`, `plugins/*`) | Unit / integration | |
| 163 | +| `install-dynamic-plugins` | Vitest (`scripts/install-dynamic-plugins`) | Install script unit tests | |
| 164 | +| `rhdh-e2e-frontend` (new) | Playwright `page.coverage` | E2E frontend — this doc | |
| 165 | +| `rhdh-e2e-full` (future) | Instrumented showcase image variant | E2E frontend, higher fidelity (RHIDP-13244) | |
| 166 | +| `overlays-e2e-<plugin>` (future) | CoverPort Tekton | Upstream plugins via OCI (RHIDP-11866) | |
| 167 | + |
| 168 | +## References |
| 169 | + |
| 170 | +- Playwright coverage API: https://playwright.dev/docs/api/class-coverage |
| 171 | +- monocart-coverage-reports: https://github.com/cenfun/monocart-coverage-reports |
| 172 | +- Chrome DevTools Protocol — Coverage domain |
| 173 | +- RHDH Test Strategy Proposal (Google Doc): https://docs.google.com/document/d/1B-Jl1uwX3sdWOGqs9CN9rTFYH743q-o5YVMoAz_yPh8/edit |
0 commit comments