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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | 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 1x 1x 1x 1x 1x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 4928x 2994x 4928x 1934x 1934x 2994x 2994x 2994x 2994x 4928x 2922x 2922x 72x 72x 72x 72x 4928x 18x 18x 54x 54x 54x 4928x 4928x 4928x 4928x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 4928x 4928x 4928x 4928x 4928x 4928x 16x 16x 16x 2774x 2774x 2994x 2994x 2994x 2774x 2774x 2774x 2774x 2774x 2774x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 10x 10x 10x 10x 10x 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) are mapped to
* file-system paths so c8 can correlate them with source files:
* - Project-relative paths like `http://localhost:5173/src/components/Portal.tsx`
* are resolved relative to the project root (cwd).
* - `@fs/` paths like `http://localhost:5173/@fs/home/.../blong-browser/src/index.ts`
* are used as-is (Vite's prefix for symlinked workspace packages).
* - `node_modules/`, `@vite/`, and `@react-refresh` URLs are excluded.
*/
import {test as base, type Page, type TestType} from '@playwright/test';
import crypto from 'node:crypto';
import {existsSync, 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
* http://localhost:5173/@fs/home/.../blong-browser/src/index.ts
*
* We strip the protocol/host/port and resolve relative to cwd.
* `@fs/` prefixed URLs are treated as absolute filesystem paths.
* Source files that are outside the project are excluded.
*/
function browserUrlToFilePath(browserUrl: string, cwd: string): string | null {
try {
const url = new URL(browserUrl);
let pathname = url.pathname;
const protocol = url.protocol;
if (protocol === 'file:') {
// A file:// URL may be:
// file:///absolute/path — 3 slashes, hostname is empty, pathname is correct
// file://home/.../file — 2 slashes, hostname="home", pathname="/.../file" (missing /home)
// Reconstruct the full path when hostname is set.
if (url.hostname) {
pathname = `/${url.hostname}${pathname}`;
}
// Some URLs embed /@fs/ (from Vite's resolved symlink prefix).
// Strip everything before the last /@fs/ to reach the real path.
const fsIdx = pathname.lastIndexOf('/@fs/');
if (fsIdx >= 0) {
return pathname.slice(fsIdx + 5);
}
// Vite may also strip /@fs/ and prepend cwd instead, e.g.
// .../blong-suite/home/user/.../blong-browser/src/...
// Strip the cwd prefix when present.
if (pathname.startsWith(cwd)) {
const suffix = pathname.slice(cwd.length);
if (suffix.startsWith('/')) return suffix;
}
return pathname;
}
// Only handle HTTP(S) URLs from the dev server
if (protocol !== 'http:' && protocol !== 'https:') {
return null;
}
// Exclude Vite virtual modules (/@id/…, /@react-refresh),
// bundled dependencies (/@vite/), node_modules, and non-source assets
if (
pathname.includes('/@id/') ||
pathname.includes('/@react-refresh') ||
pathname.includes('/@vite/') ||
pathname.includes('/node_modules/') ||
pathname.startsWith('/favicon')
) {
return null;
}
// Handle /@fs/ prefix — the rest is an absolute filesystem path.
// Strip /@fs (4 chars) not /@fs/ (5 chars) to keep the leading /,
// so /@fs/home/user/... becomes /home/user/... (correct absolute path).
if (pathname.startsWith('/@fs/')) {
return pathname.slice(4);
}
// Some URLs are absolute filesystem paths without the /@fs/ prefix,
// e.g. http://localhost:5173/home/user/.../blong-browser/src/...
// Detect by checking if the path exists on disk.
if (pathname.startsWith('/') && existsSync(pathname)) {
return pathname;
}
// Regular project-relative path: resolve against cwd
// (e.g. /src/components/Portal.tsx → cwd + /src/components/Portal.tsx)
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);
|