Files
crumbs/tests/e2e/helpers/coverage.ts
T
Frederic CabassutandClaude Opus 4.6 a724770af4 feat: add code coverage with artifact upload for unit and E2E tests
Add V8 coverage for unit tests via @vitest/coverage-v8 and Istanbul
instrumentation for E2E tests via vite-plugin-istanbul. Coverage data
is collected from browser pages after each Playwright test and merged
with nyc. CI uploads both coverage reports as GitHub Actions artifacts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 09:30:08 +01:00

25 lines
833 B
TypeScript

import { writeFileSync, mkdirSync } from 'fs';
import { join } from 'path';
import type { Page } from '@playwright/test';
const COVERAGE_DIR = join(process.cwd(), 'coverage', 'e2e', 'raw');
let counter = 0;
/**
* Collect Istanbul coverage data from the browser page.
* The vite-plugin-istanbul injects `window.__coverage__` when COVERAGE=true.
*/
export async function collectCoverage(page: Page, testTitle: string) {
if (process.env.VITE_COVERAGE !== 'true') return;
const coverage = await page.evaluate(() => (window as any).__coverage__);
if (!coverage) return;
mkdirSync(COVERAGE_DIR, { recursive: true });
const safeName = testTitle.replace(/[^a-zA-Z0-9-_]/g, '_');
const filename = `coverage-${safeName}-${counter++}-${Date.now()}.json`;
writeFileSync(join(COVERAGE_DIR, filename), JSON.stringify(coverage));
}