Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5520x 5520x 5520x 5520x 5520x 5520x 5520x 5520x 5520x 5520x 5520x 3390x 5520x 2130x 2130x 3390x 5520x 5520x 5520x 5520x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 5520x 5520x 5520x 5520x 5520x 5520x 18x 18x 18x 3168x 3168x 3390x 3390x 3390x 3168x 3168x 3168x 3168x 3168x 3168x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Playwright coverage fixture for blong-browser tests.
*
* Collects browser-side V8 JavaScript coverage via Playwright's built-in
* `page.coverage` API and writes it in the format expected by `c8`.
*
* Usage in a test file:
* ```ts
* import {test} from '@feasibleone/blong-browser/playwright/coverage';
* // or compose with the portal fixture:
* import {test} from '@feasibleone/blong-browser/playwright';
* import {coverageFixture} from '@feasibleone/blong-browser/playwright/coverage';
* coverageFixture(test);
* ```
*
* The fixture writes coverage data to the directory specified by the
* `NODE_V8_COVERAGE` environment variable (set automatically when using
* `blong-dev playwright --coverage`). This data joins the server-side
* V8 coverage from the blong server process, and both are aggregated
* by `c8 report`.
*
* Browser coverage URLs (from Vite dev server, e.g.
* `http://localhost:5173/src/components/Portal.tsx`) are mapped to
* file-system paths relative to the project root so c8 can correlate
* them with source files.
*/
import {test as base, type Page, type TestType} from '@playwright/test';
import crypto from 'node:crypto';
import {mkdirSync, writeFileSync} from 'node:fs';
import {join} from 'node:path';
interface IV8CoverageEntry {
scriptId: string;
url: string;
functions: Array<{
functionName: string;
isBlockCoverage: boolean;
ranges: Array<{startOffset: number; endOffset: number; count: number}>;
}>;
}
interface IV8CoverageFile {
result: IV8CoverageEntry[];
}
/**
* Map a browser script URL (typically from Vite dev server) to a
* file-system path that c8 can resolve.
*
* Vite dev server URLs look like:
* http://localhost:5173/src/components/Portal.tsx
* http://localhost:5173/node_modules/.vite/deps/react.js
*
* We strip the protocol/host/port and resolve relative to cwd.
* Source files that are outside the project are excluded.
*/
function browserUrlToFilePath(browserUrl: string, cwd: string): string | null {
try {
const url = new URL(browserUrl);
// Only handle HTTP URLs from the dev server
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
// file:// URLs can be used as-is
if (url.protocol === 'file:') {
return url.pathname;
}
return null;
}
const pathname = url.pathname;
// Exclude Vite client, HMR, node_modules
if (
pathname.includes('/node_modules/') ||
pathname.includes('/@vite/') ||
pathname.includes('/@react-refresh') ||
pathname.startsWith('/favicon')
) {
return null;
}
// Strip leading / and resolve against cwd
const relPath = pathname.startsWith('/') ? pathname.slice(1) : pathname;
return join(cwd, relPath);
} catch {
return null;
}
}
/**
* Write a single browser coverage entry as a V8 coverage JSON file
* in the directory pointed to by NODE_V8_COVERAGE.
*/
function writeBrowserCoverage(coverage: IV8CoverageEntry[], v8Dir: string): void {
if (coverage.length === 0) return;
mkdirSync(v8Dir, {recursive: true});
// Group entries by file path (multiple scriptId entries may map to the same file)
const byFile = new Map<string, IV8CoverageEntry[]>();
const cwd = process.cwd();
for (const entry of coverage) {
const filePath = browserUrlToFilePath(entry.url, cwd);
if (!filePath) continue;
const group = byFile.get(filePath) ?? [];
group.push(entry);
byFile.set(filePath, group);
}
// Write one V8 coverage JSON file per unique file
for (const [filePath, entries] of byFile) {
const v8File: IV8CoverageFile = {
result: entries.map(e => ({
scriptId: e.scriptId,
url: `file://${filePath}`,
functions: e.functions,
})),
};
const hash = crypto.createHash('md5').update(filePath).digest('hex').slice(0, 12);
const outFile = join(v8Dir, `browser-${hash}.json`);
writeFileSync(outFile, JSON.stringify(v8File));
}
}
/**
* Creates a new test object extended with browser-side coverage collection.
*
* The fixture runs automatically (`auto: true`) — it starts JS coverage
* collection at the beginning of each test and writes the results to
* `NODE_V8_COVERAGE` directory after the test completes.
*
* @param testType - The Playwright test object to extend.
* @returns A new test object with the coverage fixture added.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
export function coverageFixture<
TestArgs extends Record<string, any>,
WorkerArgs extends Record<string, any>,
>(testType: TestType<TestArgs, WorkerArgs>): TestType<TestArgs, WorkerArgs> {
return (testType as any).extend({
collectBrowserCoverage: [
async ({page}: {page: Page}, useFixture: () => Promise<void>) => {
const v8Dir = process.env['NODE_V8_COVERAGE'];
if (!v8Dir) {
// Not running under --coverage mode; skip silently
await useFixture();
return;
}
try {
// Use resetOnNavigation: false so coverage accumulates
// across navigation (login → test actions)
await page.coverage.startJSCoverage({
resetOnNavigation: false,
});
} catch {
// coverage API may not be available in all browsers (Chromium only)
await useFixture();
return;
}
let coverage: IV8CoverageEntry[] = [];
try {
await useFixture();
coverage =
(await page.coverage.stopJSCoverage()) as unknown as IV8CoverageEntry[];
} catch {
// Swallow errors during coverage stop
}
if (coverage.length > 0) {
writeBrowserCoverage(coverage, v8Dir);
}
},
{auto: true, scope: 'test'},
],
}) as TestType<TestArgs, WorkerArgs>;
}
/* eslint-enable @typescript-eslint/no-explicit-any */
/**
* Pre-extended test with browser-side coverage collection enabled.
*
* Use this instead of importing from `@feasibleone/blong-browser/playwright`
* when you want to collect browser JS coverage during test runs.
*
* ```ts
* import {test, expect} from '@feasibleone/blong-browser/playwright/coverage';
* ```
*
* Note: This test does NOT include the `portal` fixture. Compose fixtures
* when you need both coverage and portal support:
* ```ts
* import {test as baseTest, expect} from '@feasibleone/blong-browser/playwright';
* import {coverageFixture} from '@feasibleone/blong-browser/playwright/coverage';
* const test = coverageFixture(baseTest);
* ```
*/
export const test = coverageFixture(base);
|