Skip to content

bun test: run afterAll hooks when --bail stops a run - #35504

Closed
robobun wants to merge 4 commits into
mainfrom
farm/d155d83a/bail-runs-afterall
Closed

robobun wants to merge 4 commits into
mainfrom
farm/d155d83a/bail-runs-afterall

Conversation

@robobun

@robobun robobun commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #12250.

Problem

bun test --bail exits via Global::exit(1) directly from the per-test result callback (CommandLineReporter::handle_test_completed), which skips every remaining execution group including the afterAll hooks for describe scopes that were already on the stack. Only the failing test's afterEach fires; servers, temp directories, and other fixtures started in beforeAll are leaked on every bailed run.

import { describe, test, afterAll, afterEach } from "bun:test";
afterAll(() => console.log("ROOT-AFTERALL"));
describe("D", () => {
  afterAll(() => console.log("DESCRIBE-AFTERALL"));   // server.close() / rm -rf tmpdir
  afterEach(() => console.log("AFTEREACH"));
  test("f1", () => { throw new Error("x"); });
  test("f2", () => {});
});
// bun test        -> AFTEREACH, ..., DESCRIBE-AFTERALL, ROOT-AFTERALL
// bun test --bail -> AFTEREACH, "Bailed out after 1 failure"   (neither afterAll runs)

Jest's --bail finishes the current file including all afterAll hooks (it bails between files only).

Fix

Instead of exiting in place, latch the bail on the Execution and let step_group finish the file:

  • ConcurrentGroup now records scope_start (the group index where its enclosing describe scope begins) and is_after_all, populated during order generation.
  • When summary.fail == bail, handle_test_completed sets reporter.bailed = true and execution.bail_at_group = Some(group_index) instead of exiting.
  • Once bail_at_group is set, step_group skips every group that is not an afterAll whose scope_start <= bail_at_group (i.e. only the scopes already entered when bail fired get their teardown). step_sequence_one drops not-yet-run test sequences in the current group so concurrent groups settle without starting new tests.
  • The summary line and Bailed out after N failures message move to TestCommand::run, printed after the file's afterAll unwind completes, then Global::exit(1) as before.

With the fix the example above now prints AFTEREACH, DESCRIBE-AFTERALL, ROOT-AFTERALL, then the bail message, and still exits 1 having run only the one failing test. afterAll for sibling describes that had not started yet does not run.

Verification

# fails on main (no afterAll lines), passes with this change
bun bd test test/cli/test/bun-test.test.ts -t "runs afterAll for the active describe stack"

Existing --bail, hook, describe, and concurrent suites pass unchanged.


no test proof · iteration 0 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/cli/test/bun-test.test.ts

Not covered here

Two narrow same-class cases are intentionally left for a follow-up; neither is a regression (pre-PR ran zero afterAll under --bail):

  • Preload-level afterAll in serial multi-file runs: an afterAll registered from a --preload script is only scheduled into the last file's execution order (gated on first_last.last). When bail fires in an earlier file the loop in run_all_tests never reaches the last file, so the preload afterAll still does not run. --isolate and --parallel are unaffected (every file is last there). Covering this needs a different mechanism than the per-file unwind built here.
  • test.concurrent merged across a describe boundary: when a nested describe with no beforeAll starts with test.concurrent immediately after an outer test.concurrent, the nested test merges into the outer group, so the nested afterAll's scope_start exceeds the merged group index and the unwind predicate skips it. Breaking the merge at describe entry regresses the documented concurrent ordering (concurrent.fixture.ts), so this is left as-is.

When --bail hits its threshold, handle_test_completed was calling
Global::exit(1) directly from inside on_sequence_completed, which skips
every remaining group in the execution order, including the afterAll
hooks for describe scopes that were already on the stack. Servers,
temp directories, and other fixtures started in beforeAll were leaked
on every bailed run.

Instead of exiting in place, latch the bail on the Execution and let
step_group finish the file: each ConcurrentGroup now records the group
index where its enclosing describe scope begins and whether it holds
afterAll entries, so once bail fires the step loops can skip every
remaining test/beforeAll group and run only the afterAll groups whose
scope_start is at or before the group where bail fired (the scopes
already entered). step_sequence_one drops not-yet-run test sequences
in the current group so concurrent groups settle without starting new
tests. The summary line and "Bailed out after N failures" message
move to TestCommand::run, after the file's afterAll unwind completes.

Fixes #12250
@robobun

robobun commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 11:26 PM PT - Jul 24th, 2026

❌ @robobun, your commit aea35d9 has some failures in Build #80022 (All Failures)


🧪   To try this PR locally:

bunx bun-pr 35504

That installs a local version of the PR into your bun-35504 executable, so you can run:

bun-35504 --bun

@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 10 seconds

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 95180644-4f68-4cb0-b67d-6be0cc3de9a2

📥 Commits

Reviewing files that changed from the base of the PR and between ae4b17d and aea35d9.

📒 Files selected for processing (6)
  • src/runtime/cli/test_command.rs
  • src/runtime/test_runner/Execution.rs
  • src/runtime/test_runner/Order.rs
  • src/runtime/test_runner/bun_test.rs
  • test/cli/test/bun-test.test.ts
  • test/regression/issue/12250.test.ts

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

Found 1 issue this PR may fix:

  1. bun test exits after failures are not LSan-clean (bail and failing runs abort under detect_leaks=1) #32183 - Mechanism 1 (bail branch calling Global::exit(1) directly without teardown) is directly addressed by this PR's change to latch a flag and run afterAll hooks before exiting

If this is helpful, copy the block below into the PR description to auto-close this issue on merge.

Fixes #32183

🤖 Generated with Claude Code

@robobun

robobun commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator Author

Re #32183: this moves the bail exit past the file's completion (so exit_file() now runs and no BunTest::run frames are live), which removes the safety concern noted there for mechanism 1, but I kept Global::exit(1) rather than the full global_exit() teardown so the LSan cleanliness itself is not addressed. Mechanisms 2 and 3 are untouched. Not adding the Fixes line.

Comment thread src/runtime/cli/test_command.rs Resolved
Comment thread src/runtime/test_runner/Order.rs Resolved
Comment thread src/runtime/test_runner/Execution.rs Resolved
A concurrent test whose beforeEach/body already ran should still run its
afterEach chain and reach on_sequence_completed; gate the bail drop on
active_entry == first_entry so only sequences that have not started at
all are discarded.
@robobun

robobun commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator Author

CI on build 80022: all five failures are tagged [flaky] (also failing on main) and unrelated to this change:

  • test/js/bun/terminal/terminal-platform-gaps.test.ts (win2019 x64, ConPTY output shape)
  • test/cli/install/bun-install-registry.test.ts (win11 aarch64, peer hoisting)
  • test/js/node/test/parallel/test-repl-close.js (win2019 x64, EPIPE)
  • test/js/node/test/parallel/test-tls-hello-parser-failure.js (win11 aarch64, timeout)
  • test/cli/run/filter-workspace.test.ts (alpine x64, elide output)

The new --bail tests (bun-test.test.ts, regression/issue/12250.test.ts) pass on every lane. Ready for review.

@robobun

robobun commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as part of a cleanup of stale pull requests. This PR has had no new commits since 2026-07-25, it conflicts with main, and its last CI run failed. This is not a judgment on the fix itself. The linked issue (#12250) stays open. If the problem still reproduces on a current build, reopen this PR after a rebase or open a new one against main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bun:test with --bail and afterAll hook

2 participants