What happens
onTestFinished() throws at runtime when called inside a concurrent test:
Cannot call onTestFinished() here. It cannot be called inside a concurrent test. Use test.serial or remove test.concurrent.
Why this is surprising
A plain try/finally cleanup inside the same concurrent test works fine, so from a user perspective onTestFinished() looks like it should be a structured/lifecycle version of the same cleanup pattern.
In practice, this means that code like this works:
import { test } from "bun:test";
test("concurrent cleanup with try/finally", async () => {
const dir = await Promise.resolve("tmp");
try {
// test body
} finally {
await Promise.resolve();
}
});
but the equivalent onTestFinished() version does not:
import { expect, onTestFinished, test } from "bun:test";
test("concurrent cleanup with onTestFinished", async () => {
onTestFinished(async () => {
await Promise.resolve();
});
expect(1).toBe(1);
});
Repro
import { expect, onTestFinished, test } from "bun:test";
test("onTestFinished in concurrent test", async () => {
onTestFinished(async () => {
await Promise.resolve();
});
expect(1).toBe(1);
});
Run with:
Expected
Either:
onTestFinished() works in concurrent tests, since try/finally cleanup in the same test already does, or
- if this is intentionally unsupported, the restriction should be documented consistently across the API docs.
Docs note
The lifecycle docs page does mention that onTestFinished() is not supported in concurrent tests, but the generated TypeScript API docs/comments in bun-types/test.d.ts do not mention that restriction. If the behavior is intentional, it would help to surface that limitation there too so it is visible in editor hover/docs and not only after a runtime error.
Environment
- Bun:
1.3.12
- Platform:
Darwin xpmbp23 25.3.0 arm64
What happens
onTestFinished()throws at runtime when called inside a concurrent test:Why this is surprising
A plain
try/finallycleanup inside the same concurrent test works fine, so from a user perspectiveonTestFinished()looks like it should be a structured/lifecycle version of the same cleanup pattern.In practice, this means that code like this works:
but the equivalent
onTestFinished()version does not:Repro
Run with:
bun test --concurrentExpected
Either:
onTestFinished()works in concurrent tests, sincetry/finallycleanup in the same test already does, orDocs note
The lifecycle docs page does mention that
onTestFinished()is not supported in concurrent tests, but the generated TypeScript API docs/comments inbun-types/test.d.tsdo not mention that restriction. If the behavior is intentional, it would help to surface that limitation there too so it is visible in editor hover/docs and not only after a runtime error.Environment
1.3.12Darwin xpmbp23 25.3.0 arm64