diff --git a/e2e/coverage.ts b/e2e/coverage.ts deleted file mode 100644 index 319009ee..00000000 --- a/e2e/coverage.ts +++ /dev/null @@ -1,30 +0,0 @@ -import fs from "node:fs"; -import path from "node:path"; -import type { Page } from "@playwright/test"; - -const OUTPUT_DIR = path.resolve(process.cwd(), ".nyc_output"); - -/** - * Reads the istanbul coverage object (injected by vite-plugin-istanbul) from the - * given page. Returns `null` when the page has not been instrumented. - */ -export async function readCoverage(page: Page): Promise { - try { - return await page.evaluate(() => (window as unknown as { __coverage__?: unknown }).__coverage__ ?? null); - } catch { - // Page might be navigating/closed. - return null; - } -} - -/** - * Persists a coverage chunk to `.nyc_output` so that `nyc report` can merge it. - * istanbul-lib-coverage (used by nyc) sums the hit counts across every file it - * finds, so writing one file per chunk is enough to accumulate coverage across - * navigations and tests. - */ -export function writeCoverage(coverage: unknown, id: string): void { - if (!coverage) return; - fs.mkdirSync(OUTPUT_DIR, { recursive: true }); - fs.writeFileSync(path.join(OUTPUT_DIR, `playwright-${id}.json`), JSON.stringify(coverage)); -} diff --git a/e2e/fixtures.ts b/e2e/fixtures.ts index 4ffd0dd5..e8e5fdfe 100644 --- a/e2e/fixtures.ts +++ b/e2e/fixtures.ts @@ -1,5 +1,6 @@ import { test, expect, type Page } from "@playwright/test"; -import { readCoverage, writeCoverage } from "./coverage"; +import fs from "node:fs"; +import path from "node:path"; let activePage: Page | undefined; const coverageChunks: unknown[] = []; @@ -12,6 +13,33 @@ export function currentPage(): Page { return activePage; } +const OUTPUT_DIR = path.resolve(process.cwd(), ".nyc_output"); + +/** + * Reads the istanbul coverage object (injected by vite-plugin-istanbul) from the + * given page. Returns `null` when the page has not been instrumented. + */ +export async function readCoverage(page: Page): Promise { + try { + return await page.evaluate(() => (window as unknown as { __coverage__?: unknown }).__coverage__ ?? null); + } catch { + // Page might be navigating/closed. + return null; + } +} + +/** + * Persists a coverage chunk to `.nyc_output` so that `nyc report` can merge it. + * istanbul-lib-coverage (used by nyc) sums the hit counts across every file it + * finds, so writing one file per chunk is enough to accumulate coverage across + * navigations and tests. + */ +export function writeCoverage(coverage: unknown, id: string): void { + if (!coverage) return; + fs.mkdirSync(OUTPUT_DIR, { recursive: true }); + fs.writeFileSync(path.join(OUTPUT_DIR, `playwright-${id}.json`), JSON.stringify(coverage)); +} + /** Records a coverage snapshot (called before navigations, which reset __coverage__). */ export function recordCoverageChunk(chunk: unknown): void { if (chunk) coverageChunks.push(chunk);