Skip to content

fix(test): bail correctly halts current run in watch mode #19918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/bun.js/test/jest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const expect = @import("./expect.zig");
const Counter = expect.Counter;
const Expect = expect.Expect;


const JSC = bun.JSC;

const logger = bun.logger;
Expand Down Expand Up @@ -43,6 +42,11 @@ var max_test_id_for_debugger: u32 = 0;
pub const TestRunner = struct {
tests: TestRunner.Test.List = .{},
log: *logger.Log,
/// True when the current test run is executed under `--watch`
is_watch_mode: bool = false,
/// Runtime flag set by the reporter to abort the current run early
/// (used for `--bail` handling in watch mode)
should_abort: bool = false,
files: File.List = .{},
index: File.Map = File.Map{},
only: bool = false,
Expand Down
41 changes: 37 additions & 4 deletions src/cli/test_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const default_allocator = bun.default_allocator;

const std = @import("std");


const FileSystem = @import("../fs.zig").FileSystem;
const options = @import("../options.zig");
const js_ast = bun.JSAst;
Expand Down Expand Up @@ -651,10 +650,19 @@ pub const CommandLineReporter = struct {
this.summary.expectations += expectations;
this.jest.tests.items(.status)[id] = TestRunner.Test.Status.fail;

if (this.jest.bail == this.summary.fail) {
if (this.jest.bail > 0 and this.jest.bail == this.summary.fail) {
this.printSummary();
Output.prettyError("\nBailed out after {d} failure{s}<r>\n", .{ this.jest.bail, if (this.jest.bail == 1) "" else "s" });
Global.exit(1);

// If in watch mode, set a flag to stop the current run instead of exiting.
// Otherwise, exit as before.
// This assumes 'is_watch_mode' and 'should_abort' fields are added to TestRunner struct.
if (this.jest.is_watch_mode) {
Output.prettyError("--bail active in --watch mode: Stopping current test run. Waiting for file changes...\n", .{});
this.jest.should_abort = true;
} else {
Global.exit(1);
}
}
}

Expand Down Expand Up @@ -1220,6 +1228,8 @@ pub const TestCommand = struct {
else => {},
}

// Set is_watch_mode on the TestRunner instance
reporter.jest.is_watch_mode = (vm.hot_reload == .watch);
runAllTests(reporter, vm, test_files, ctx.allocator);
}

Expand Down Expand Up @@ -1452,15 +1462,23 @@ pub const TestCommand = struct {
const allocator = this.allocator;
bun.assert(files.len > 0);

// Reset should_abort flag at the beginning of each full test suite execution
reporter.jest.should_abort = false;

if (files.len > 1) {
for (files[0 .. files.len - 1]) |file_name| {
if (reporter.jest.should_abort) {
break;
}
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 |err| handleTopLevelTestErrorBeforeJavaScriptStart(err);
if (!reporter.jest.should_abort) {
TestCommand.run(reporter, vm, files[files.len - 1].slice(), allocator, true) catch |err| handleTopLevelTestErrorBeforeJavaScriptStart(err);
}
}
};

Expand Down Expand Up @@ -1516,6 +1534,10 @@ pub const TestCommand = struct {
vm.onUnhandledRejection = jest.TestRunnerTask.onUnhandledRejection;

while (repeat_index < repeat_count) : (repeat_index += 1) {
if (reporter.jest.should_abort) {
break;
}

if (repeat_count > 1) {
Output.prettyErrorln("<r>\n{s}{s}: <d>(run #{d})<r>\n", .{ file_prefix, file_title, repeat_index + 1 });
} else {
Expand Down Expand Up @@ -1557,6 +1579,9 @@ pub const TestCommand = struct {
const file_end = reporter.jest.files.len;

for (file_start..file_end) |module_id| {
if (reporter.jest.should_abort) {
break;
}
const module: *jest.DescribeScope = reporter.jest.files.items(.module_scope)[module_id];

vm.onUnhandledRejectionCtx = null;
Expand Down Expand Up @@ -1596,6 +1621,14 @@ pub const TestCommand = struct {
_ = vm.global.vm().runGC(false);
},
}

if (reporter.jest.should_abort) { // Check again in case it was set during module.runTests
break;
}
}

if (reporter.jest.should_abort) { // Check after processing all modules in a file
break;
}

vm.global.handleRejectedPromises();
Expand Down