日本語

Fixing bun test Timeout Failures in Bun

​​Bun​Testing,Troubleshooting,TypeScript

When bun test fails with "this test timed out after 5000ms.", one test did not finish within Bun's default limit of 5 seconds (5000ms). If the work really takes longer than 5 seconds, raising that test's limit makes the error go away. If the cause is a missing done call or waiting for an event that never comes, raising the limit does not help; you need to fix the test code.

This is for the situation where tests that call an API or prepare a database start failing with timeouts, both locally and in CI. I deliberately triggered this error for each cause in a test environment, recorded the messages, and confirmed that fixing each cause made the error go away.

Note

The test environment was Ubuntu 24.04 with Bun 1.4.4. The tests were written in TypeScript and used only bun:test, with no packages installed. Slow work was simulated by waiting 6 seconds with setTimeout or Bun.sleep. I did not test Windows or macOS, other Bun versions, settings in bunfig.toml, or tests that use fake timers (jest.useFakeTimers).

Tell the cause apart by the rest of the message

In the test environment, the message differed by cause in the following three ways.

Message Cause
this test timed out after 5000ms. The work took longer than 5 seconds, or the test is waiting forever for an event or Promise that never arrives
this test timed out after 5000ms, before its done callback was called. … The test function takes done as a parameter but never calls it
a beforeEach/afterEach hook timed out for this test. A hook that runs before or after the test (such as beforeAll) took longer than 5 seconds

The first message looks the same whether the work is genuinely slow or the test is waiting for something that will never happen. First, check what the test is waiting for.

If the work really is slow, raise the limit

I ran a test that waits for a report aggregation that takes 6 seconds.

ts
// tests/report.test.ts
import { test, expect } from "bun:test";

// Stand-in for an aggregation that takes 6 seconds
function buildReport(): Promise<string> {
  return new Promise((resolve) => setTimeout(() => resolve("ok"), 6000));
}

test("月次の集計を作る", async () => {
  const result = await buildReport();
  expect(result).toBe("ok");
});
text
$ bun test ./tests/report.test.ts
bun test v1.4.4 (38afee6c8)

tests/report.test.ts:
(fail) 月次の集計を作る [5001.74ms]
  ^ this test timed out after 5000ms.

 0 pass
 1 fail
Ran 1 test across 1 file. [5.00s]

The exit code was 1, so the test was treated as a failure. In CI, it fails the same way.

To raise the limit for just that test, pass the number of milliseconds as the third argument to test (the official documentation section on per-test timeouts). As in the example in the test type definitions, you can also pass an options object such as { timeout: 10000 }.

ts
// tests/report-arg.test.ts (buildReport is the same as in report.test.ts)
test("月次の集計を作る", async () => {
  const result = await buildReport();
  expect(result).toBe("ok");
}, 10000);
text
$ bun test ./tests/report-arg.test.ts
bun test v1.4.4 (38afee6c8)

 1 pass
 0 fail
 1 expect() calls
Ran 1 test across 1 file. [6.00s]

It finished in 6 seconds and the error was gone. A file that passed { timeout: 10000 } as the third argument also passed.

When a file has several slow tests, call setDefaultTimeout at the top of the file to change the default limit for the tests in that file. The type definition of setDefaultTimeout states that a limit given to an individual test takes precedence.

ts
import { test, expect, setDefaultTimeout } from "bun:test";

setDefaultTimeout(10000);

I changed the top of the original test file to this and ran it. It passed in 6 seconds.

To raise the limit for tests in every file, add --timeout to bun test. I ran the original report.test.ts without changing it, and it passed.

text
$ bun test --timeout 10000 ./tests/report.test.ts
bun test v1.4.4 (38afee6c8)

 1 pass
 0 fail
 1 expect() calls
Ran 1 test across 1 file. [6.01s]

bun test --help describes --timeout as "Set the per-test timeout in milliseconds, default is 5000." It changes the limit for each individual test at once, not the total time for the whole run (the usage is also covered in the official documentation section on timeouts).

The test takes done but never calls it

If you add a parameter (done) to a test function, Bun does not consider the test finished until done is called (the official documentation on done). In this test that receives an event, I forgot to call done.

ts
// tests/done.test.ts
import { test, expect } from "bun:test";
import { EventEmitter } from "node:events";

test("保存のイベントを受け取る", (done) => {
  const bus = new EventEmitter();
  bus.on("saved", (id: number) => {
    expect(id).toBe(1);
  });
  bus.emit("saved", 1);
});
text
$ bun test ./tests/done.test.ts
bun test v1.4.4 (38afee6c8)

tests/done.test.ts:
(fail) 保存のイベントを受け取る [5000.02ms]
  ^ this test timed out after 5000ms, before its done callback was called. If a done callback was not intended, remove the last parameter from the test callback function

 0 pass
 1 fail
 1 expect() calls
Ran 1 test across 1 file. [5.00s]

expect ran once (1 expect() calls), and the value it checked was correct. But because done was never called, the test waited 5 seconds and then failed. The second half of the message means: "If you did not intend to use done, remove the last parameter from the test callback function."

When I called done() after the assertion, the test passed in 5 milliseconds.

ts
  bus.on("saved", (id: number) => {
    expect(id).toBe(1);
    done();
  });
text
$ bun test ./tests/done-fixed.test.ts
bun test v1.4.4 (38afee6c8)

 1 pass
 0 fail
 1 expect() calls
Ran 1 test across 1 file. [5.00ms]

The event fired before the test started waiting

If a process that should finish immediately reports only this test timed out after 5000ms., the thing being waited for may never arrive. In the next test, the event is emitted first and only then does the test start waiting with once.

ts
// tests/event.test.ts
import { test, expect } from "bun:test";
import { EventEmitter, once } from "node:events";

test("保存のイベントを待つ", async () => {
  const bus = new EventEmitter();
  bus.emit("saved", 1);
  const [id] = await once(bus, "saved");
  expect(id).toBe(1);
});
text
$ bun test ./tests/event.test.ts
bun test v1.4.4 (38afee6c8)

tests/event.test.ts:
(fail) 保存のイベントを待つ [5000.02ms]
  ^ this test timed out after 5000ms.

 0 pass
 1 fail
Ran 1 test across 1 file. [5.00s]

As the Node.js documentation for emitter.emit describes, emit only calls the listeners registered at that moment. Here no listener existed yet, so nobody received the event. Starting to wait afterward means the next saved event never comes. Raising the limit does not fix this case: when I ran it with --timeout 10000, it waited 10 seconds and failed the same way.

text
$ bun test --timeout 10000 ./tests/event.test.ts
bun test v1.4.4 (38afee6c8)

tests/event.test.ts:
(fail) 保存のイベントを待つ [10000.01ms]
  ^ this test timed out after 10000ms.

 0 pass
 1 fail
Ran 1 test across 1 file. [10.00s]

When I created the Promise that waits with once first and emitted the event afterward, the test passed in 5 milliseconds.

ts
test("保存のイベントを待つ", async () => {
  const bus = new EventEmitter();
  const saved = once(bus, "saved");
  bus.emit("saved", 1);
  const [id] = await saved;
  expect(id).toBe(1);
});
text
$ bun test ./tests/event-fixed.test.ts
bun test v1.4.4 (38afee6c8)

 1 pass
 0 fail
 1 expect() calls
Ran 1 test across 1 file. [5.00ms]

When beforeAll exceeds 5 seconds, the message changes

When I waited 6 seconds in beforeAll, which runs once before the tests, the message was as follows.

ts
// tests/setup.test.ts
import { beforeAll, test, expect } from "bun:test";

let ready = false;

beforeAll(async () => {
  await Bun.sleep(6000); // Stand-in for 6 seconds of setup
  ready = true;
});

test("準備のあとに動くテスト", () => {
  expect(ready).toBe(true);
});
text
$ bun test ./tests/setup.test.ts
bun test v1.4.4 (38afee6c8)

tests/setup.test.ts:
(fail) (unnamed) [5001.38ms]
  ^ a beforeEach/afterEach hook timed out for this test.

 0 pass
 1 fail
Ran 1 test across 1 file. [5.00s]

I used beforeAll, but the message said beforeEach/afterEach. The test name also shows as (unnamed), so this output does not tell you which test's setup got stuck. When you see this form of message, look for slow work inside beforeAll, beforeEach, afterEach, or afterAll.

beforeAll also accepts a limit as its second argument (the beforeAll type definition). When I passed 10000, it passed in 6 seconds.

ts
beforeAll(async () => {
  await Bun.sleep(6000);
  ready = true;
}, 10000);
text
$ bun test ./tests/setup-fixed.test.ts
bun test v1.4.4 (38afee6c8)

 1 pass
 0 fail
 1 expect() calls
Ran 1 test across 1 file. [6.00s]

Even after a timeout, the test's code kept running

The official documentation says a test that times out is stopped and marked as failed (the official documentation section on per-test timeouts). However, in my test environment, the rest of a test that was waiting on await continued to run after the timeout.

ts
// tests/after-time.test.ts
import { test, expect } from "bun:test";

const start = performance.now();
const t = () => `${Math.round(performance.now() - start)}ms`;

test("止まったテスト", async () => {
  await Bun.sleep(6000);
  console.log("止まったテストの続き", t());
});

test("次のテスト", async () => {
  console.log("次のテストの始まり", t());
  await Bun.sleep(2000);
  console.log("次のテストの終わり", t());
  expect(1).toBe(1);
});
text
$ bun test ./tests/after-time.test.ts
bun test v1.4.4 (38afee6c8)

tests/after-time.test.ts:
(fail) 止まったテスト [5002.04ms]
  ^ this test timed out after 5000ms.
次のテストの始まり 5003ms
止まったテストの続き 6004ms
次のテストの終わり 7005ms

 1 pass
 1 fail
 1 expect() calls
Ran 2 tests across 1 file. [7.01s]

The first test was marked as failed at 5 seconds, and the next test started. Yet at 6 seconds, the console.log in the continuation of the first test ran, while the next test was still waiting for 2 seconds. The continuation of the waiting work ran during the same time as the next test. When a timeout appears, do not ignore it: either raise the limit or fix the way the test waits so the timeout does not occur.

​Posted by 86