fix(ci): fail on fatal test runner output

This commit is contained in:
Vincent Koc
2026-03-19 09:52:00 -07:00
parent 0a8885d6c1
commit 51519b4086
3 changed files with 90 additions and 6 deletions

View File

@@ -0,0 +1,35 @@
const DEFAULT_OUTPUT_CAPTURE_LIMIT = 200_000;
const fatalOutputPatterns = [
/FATAL ERROR:.*heap out of memory/i,
/Allocation failed - JavaScript heap out of memory/i,
/node::OOMErrorHandler/i,
];
export function appendCapturedOutput(current, chunk, limit = DEFAULT_OUTPUT_CAPTURE_LIMIT) {
if (!chunk) {
return current;
}
const next = `${current}${chunk}`;
if (next.length <= limit) {
return next;
}
return next.slice(-limit);
}
export function hasFatalTestRunOutput(output) {
return fatalOutputPatterns.some((pattern) => pattern.test(output));
}
export function resolveTestRunExitCode({ code, signal, output }) {
if (typeof code === "number" && code !== 0) {
return code;
}
if (signal) {
return 1;
}
if (hasFatalTestRunOutput(output)) {
return 1;
}
return code ?? 0;
}

View File

@@ -4,6 +4,7 @@ import os from "node:os";
import path from "node:path";
import { channelTestPrefixes } from "../vitest.channel-paths.mjs";
import { isUnitConfigTestFile } from "../vitest.unit-paths.mjs";
import { appendCapturedOutput, resolveTestRunExitCode } from "./test-parallel-utils.mjs";
import {
loadTestRunnerBehavior,
loadUnitTimingManifest,
@@ -740,10 +741,11 @@ const runOnce = (entry, extraArgs = []) =>
const resolvedNodeOptions = heapFlag
? `${nextNodeOptions} ${heapFlag}`.trim()
: nextNodeOptions;
let output = "";
let child;
try {
child = spawn(pnpm, args, {
stdio: "inherit",
stdio: ["inherit", "pipe", "pipe"],
env: { ...process.env, VITEST_GROUP: entry.name, NODE_OPTIONS: resolvedNodeOptions },
shell: isWindows,
});
@@ -753,17 +755,26 @@ const runOnce = (entry, extraArgs = []) =>
return;
}
children.add(child);
child.stdout?.on("data", (chunk) => {
const text = chunk.toString();
output = appendCapturedOutput(output, text);
process.stdout.write(chunk);
});
child.stderr?.on("data", (chunk) => {
const text = chunk.toString();
output = appendCapturedOutput(output, text);
process.stderr.write(chunk);
});
child.on("error", (err) => {
console.error(`[test-parallel] child error: ${String(err)}`);
});
child.on("exit", (code, signal) => {
child.on("close", (code, signal) => {
children.delete(child);
const resolvedCode = resolveTestRunExitCode({ code, signal, output });
console.log(
`[test-parallel] done ${entry.name} code=${String(code ?? (signal ? 1 : 0))} elapsed=${formatElapsedMs(
Date.now() - startedAt,
)}`,
`[test-parallel] done ${entry.name} code=${String(resolvedCode)} elapsed=${formatElapsedMs(Date.now() - startedAt)}`,
);
resolve(code ?? (signal ? 1 : 0));
resolve(resolvedCode);
});
});