Skip to content

onTestFinished() should work in concurrent tests (and bun-types docs should mention the current restriction) #29236

@pekeler

Description

@pekeler

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:

bun test --concurrent

Expected

Either:

  1. onTestFinished() works in concurrent tests, since try/finally cleanup in the same test already does, or
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions