fix(test): clean up vitest child process groups

This commit is contained in:
Vincent Koc
2026-04-06 18:08:58 +01:00
parent a22e44f259
commit 8301ddfa84
8 changed files with 217 additions and 3 deletions

View File

@@ -1,6 +1,10 @@
import { createRequire } from "node:module";
import path from "node:path";
import { spawnPnpmRunner } from "./pnpm-runner.mjs";
import {
installVitestProcessGroupCleanup,
shouldUseDetachedVitestProcessGroup,
} from "./vitest-process-group.mjs";
const TRUTHY_ENV_VALUES = new Set(["1", "true", "yes", "on"]);
const require = createRequire(import.meta.url);
@@ -22,18 +26,28 @@ export function resolveVitestCliEntry() {
return path.join(path.dirname(vitestPackageJson), "vitest.mjs");
}
export function resolveVitestSpawnParams(env = process.env, platform = process.platform) {
return {
env,
detached: shouldUseDetachedVitestProcessGroup(platform),
};
}
function main(argv = process.argv.slice(2), env = process.env) {
if (argv.length === 0) {
console.error("usage: node scripts/run-vitest.mjs <vitest args...>");
process.exit(1);
}
const spawnParams = resolveVitestSpawnParams(env);
const child = spawnPnpmRunner({
pnpmArgs: ["exec", "node", ...resolveVitestNodeArgs(env), resolveVitestCliEntry(), ...argv],
env,
...spawnParams,
});
const teardownChildCleanup = installVitestProcessGroupCleanup({ child });
child.on("exit", (code, signal) => {
teardownChildCleanup();
if (signal) {
process.kill(process.pid, signal);
return;
@@ -42,6 +56,7 @@ function main(argv = process.argv.slice(2), env = process.env) {
});
child.on("error", (error) => {
teardownChildCleanup();
console.error(error);
process.exit(1);
});