-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathchild_process.js
More file actions
49 lines (45 loc) · 1.77 KB
/
child_process.js
File metadata and controls
49 lines (45 loc) · 1.77 KB
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
import { spawnSync } from "node:child_process";
import path from "node:path";
const HARNESS_MODULE_PATHS = [
"features.js",
"assert.js",
"load-addon.js",
"gc.js",
"must-call.js",
"child_process.js",
].map((file) => path.join(import.meta.dirname, file));
/**
* Runs a test file in a fresh Node.js subprocess with the CTS harness globals
* pre-loaded, and returns its exit status, signal, and captured output.
*
* @param {string} filePath - Path to the JS/MJS file to execute. Resolved
* against `options.cwd` if relative.
* @param {{ cwd?: string, nodeFlags?: string[] }} [options]
* - `cwd`: working directory for the child; defaults to `process.cwd()`.
* - `nodeFlags`: CLI flags passed to `node` before the `--import` chain
* (e.g., `["--expose-gc"]`). Defaults to no flags so each caller declares
* what its child needs.
* @returns {{ status: number | null, signal: NodeJS.Signals | null, stdout: string, stderr: string }}
*/
export const spawnTest = (filePath, options = {}) => {
const args = [...(options.nodeFlags ?? [])];
for (const modulePath of HARNESS_MODULE_PATHS) {
args.push("--import", "file://" + modulePath);
}
args.push(filePath);
const result = spawnSync(process.execPath, args, {
cwd: options.cwd ?? process.cwd(),
maxBuffer: 100 * 1024 * 1024,
});
if (result.error) throw result.error;
return {
status: result.status,
signal: result.signal,
stderr: result.stderr?.toString() ?? "",
stdout: result.stdout?.toString() ?? "",
};
};
// This module is loaded in both contexts: imported by the parent test runner
// (tests.ts) and `--import`ed into every spawned child. The side effect below
// installs `spawnTest` on the child's globalThis so tests can call it directly.
Object.assign(globalThis, { spawnTest });