bun:test/require-test-timeout

Require every test to set a timeout

A test without its own timeout gets the default, which is 5 seconds unless you pass --timeout to bun test. That's a long wait when a unit test hangs, and it's too short for a test that starts a server, which then fails now and then on a slow machine.

Set the timeout as a number after the test function, as { timeout } in the options, or for all the tests below with setDefaultTimeout() or jest.setTimeout().

Tests marked .skip or .todo aren't checked.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"bun:test/require-test-timeout" = "error" # or "warn"

Examples#

example.ts1 problem
import { expect, test } from "bun:test";

test("loads the report", async () => {
  expect(await loadReport()).toBeDefined();
});
example.tsNo problems
import { expect, test } from "bun:test";

test("loads the report", async () => {
  expect(await loadReport()).toBeDefined();
}, 10_000);

test("exports the report", async () => {
  expect(await exportReport()).toBeDefined();
}, { timeout: 30_000 });
example.tsNo problems
import { expect, setDefaultTimeout, test } from "bun:test";

setDefaultTimeout(10_000);

test("loads the report", async () => {
  expect(await loadReport()).toBeDefined();
});

The timeout must be written as a non-negative number. A variable is reported, and so are options without timeout:

example.ts2 problems
import { test } from "bun:test";

const SLOW = 30_000;

test("exports the report", async () => {
  await exportReport();
}, SLOW);

test("prints the report", async () => {
  await printReport();
}, { retry: 2 });

When not to use it#

If the default timeout, or the one you pass to bun test --timeout, suits all your tests, a number on each test adds nothing.