-
Notifications
You must be signed in to change notification settings - Fork 7
/
runner-loader.ts
35 lines (29 loc) · 1.3 KB
/
runner-loader.ts
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
import { resolve } from "path";
import { parse } from "stacktrace-parser";
const stackObj = { stack: "" };
// Kinda delicate thing to separate test tap output from output logged by tests.
// Node.js doesn't know about output that happens when running tests, so we put
// them in tap comments and include their location.
for (const channel of ["stderr", "stdout"] as const) {
const ogWrite = process[channel].write;
Object.assign(process[channel], {
write(chunk: any, encoding: any, callback: any) {
Error.captureStackTrace(stackObj);
const stack = parse(stackObj.stack);
const firstNotInternal = stack.findIndex(
(s, i) => i > 0 && s.file?.startsWith("node:") === false
);
const atTestRunner = stack.findIndex((s) => s.file?.includes("node:internal/test_runner"));
// Treat this as a user log if there's an not `node:` internal log before
// the first (if any) location from `node:internal/test_runner`
if (firstNotInternal !== -1 && (atTestRunner === -1 || firstNotInternal < atTestRunner)) {
chunk = `# Log: ${JSON.stringify({
chunk: chunk.toString(),
sf: stack[firstNotInternal],
})}\n`;
}
return ogWrite.call(this, chunk, encoding, callback);
},
});
}
require(resolve(process.cwd(), process.argv[2]));