Ensure test with errors before JS execution exit with code 1 (#15321)

This commit is contained in:
Jarred Sumner
2024-11-22 01:33:58 -08:00
committed by GitHub
parent 78b495aff5
commit d01bfb5aa2
2 changed files with 21 additions and 2 deletions

View File

@@ -1599,13 +1599,13 @@ pub const TestCommand = struct {
if (files.len > 1) {
for (files[0 .. files.len - 1]) |file_name| {
TestCommand.run(reporter, vm, file_name.slice(), allocator, false) catch {};
TestCommand.run(reporter, vm, file_name.slice(), allocator, false) catch |err| handleTopLevelTestErrorBeforeJavaScriptStart(err);
reporter.jest.default_timeout_override = std.math.maxInt(u32);
Global.mimalloc_cleanup(false);
}
}
TestCommand.run(reporter, vm, files[files.len - 1].slice(), allocator, true) catch {};
TestCommand.run(reporter, vm, files[files.len - 1].slice(), allocator, true) catch |err| handleTopLevelTestErrorBeforeJavaScriptStart(err);
}
};
@@ -1769,3 +1769,12 @@ pub const TestCommand = struct {
}
}
};
fn handleTopLevelTestErrorBeforeJavaScriptStart(err: anyerror) noreturn {
if (comptime Environment.isDebug) {
if (err != error.ModuleNotFound) {
Output.debugWarn("Unhandled error: {s}\n", .{@errorName(err)});
}
}
Global.exit(1);
}

View File

@@ -5,6 +5,16 @@ import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
describe("bun test", () => {
test("running a non-existent absolute file path is a 1 exit code", () => {
const spawn = Bun.spawnSync({
cmd: [bunExe(), "test", join(import.meta.dirname, "non-existent.test.ts")],
env: bunEnv,
stdin: "ignore",
stdout: "inherit",
stderr: "inherit",
});
expect(spawn.exitCode).toBe(1);
});
test("can provide no arguments", () => {
const stderr = runTest({
args: [],