Skip to content

Instantly share code, notes, and snippets.

@AriPerkkio
Created March 27, 2026 06:19
Show Gist options
  • Select an option

  • Save AriPerkkio/0039e4c284ae35a7d81bedf71a616b76 to your computer and use it in GitHub Desktop.

Select an option

Save AriPerkkio/0039e4c284ae35a7d81bedf71a616b76 to your computer and use it in GitHub Desktop.
Record v8 coverage for repros
import inspector from "node:inspector/promises";
import { writeFileSync } from "node:fs";
const target = "coverage-target.mjs";
writeFileSync(
target,
`\
// Repro code here
`,
"utf-8",
);
const session = new inspector.Session();
const coverage = await recordCoverage(async () => {
await import(`./${target}`);
});
console.log(JSON.stringify(coverage, null, 2));
export async function recordCoverage(method: () => Promise<void>) {
session.connect();
await session.post("Profiler.enable");
await session.post("Profiler.startPreciseCoverage", {
callCount: true,
detailed: true,
});
await method();
const { result } = await session.post("Profiler.takePreciseCoverage");
await session.post("Profiler.stopPreciseCoverage");
await session.post("Profiler.disable");
session.disconnect();
return result.find(({ url }) => url.endsWith("coverage-target.mjs"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment