Skip to content

Commit ef05d27

Browse files
realjustinwuclaude
andcommitted
fix(test): keep console output off the node:test frame transport
node:test multiplexes each child's v8-serialized result frames and its raw stdout on one pipe (nodejs/node#64061, unfixed). console.* calls from code under test desync the parent's frame parser and fail the whole file with "Unable to deserialize cloned data due to invalid or unsupported version" — seen intermittently in CI (agent.routes tests). test-stdout-guard.mjs loads via NODE_OPTIONS inside each test child (NODE_TEST_CONTEXT gates it), reroutes console output to stderr — which is not part of the transport — and strips its own --import from the environment so grandchildren spawned by tests with temp cwds do not crash resolving the relative path. Co-Authored-By: Claude <[email protected]>
1 parent d816ab4 commit ef05d27

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"postbuild:server": "node scripts/promote-dist-server.mjs",
4949
"preview": "vite preview",
5050
"typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p server/tsconfig.json",
51-
"test": "tsx --tsconfig server/tsconfig.json --test \"server/**/*.test.ts\" \"server/**/*.test.js\"",
51+
"test": "cross-env NODE_OPTIONS=--import=./scripts/test-stdout-guard.mjs tsx --tsconfig server/tsconfig.json --test \"server/**/*.test.ts\" \"server/**/*.test.js\"",
5252
"test:client": "vitest run",
5353
"lint": "oxlint src/ server/",
5454
"lint:server": "oxlint server/",

‎scripts/test-stdout-guard.mjs‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// node:test runs each test file in a child process whose stdout carries the
2+
// v8-serialized result frames back to the runner — with the child's raw
3+
// console output multiplexed onto the same pipe (nodejs/node#64061). Any
4+
// console.* call from code under test can desync the runner's frame parser
5+
// and fail the whole file with "Unable to deserialize cloned data due to
6+
// invalid or unsupported version." NODE_TEST_CONTEXT is set only inside
7+
// those children; there we route console output to stderr, which is not
8+
// part of the frame transport, keeping logs visible and the stream pure.
9+
// Loaded via NODE_OPTIONS in the `test` script so every runner-spawned
10+
// child picks it up.
11+
import { format } from 'node:util';
12+
13+
if (process.env.NODE_TEST_CONTEXT) {
14+
// Drop our own --import from the environment: tests spawn grandchildren
15+
// (fake CLIs, bootstrap checks) whose cwd is usually a temp directory,
16+
// where the relative path fails to resolve and crashes them at startup.
17+
// The guard is already loaded here; nobody downstream needs it again.
18+
const ownFlag = '--import=./scripts/test-stdout-guard.mjs';
19+
if (process.env.NODE_OPTIONS?.includes(ownFlag)) {
20+
const stripped = process.env.NODE_OPTIONS
21+
.split(ownFlag)
22+
.join(' ')
23+
.replace(/\s+/g, ' ')
24+
.trim();
25+
if (stripped) {
26+
process.env.NODE_OPTIONS = stripped;
27+
} else {
28+
delete process.env.NODE_OPTIONS;
29+
}
30+
}
31+
32+
const METHODS = ['log', 'info', 'warn', 'error', 'debug', 'trace'];
33+
const wrap = (target) => {
34+
for (const method of METHODS) {
35+
if (typeof target[method] === 'function') {
36+
target[method] = (...args) => {
37+
process.stderr.write(`${format(...args)}\n`);
38+
};
39+
}
40+
}
41+
return target;
42+
};
43+
44+
// Wrap the current console, and keep wrapping whatever replaces it later:
45+
// the test child installs its own Console on top of the preloads.
46+
let current = wrap(globalThis.console);
47+
Object.defineProperty(globalThis, 'console', {
48+
configurable: true,
49+
get: () => current,
50+
set: (value) => {
51+
current = wrap(value);
52+
},
53+
});
54+
}

0 commit comments

Comments
 (0)