How to Run Tests in Parallel with test.concurrent in Bun
Tests written with test.concurrent in bun test run at the same time within the same file. In a file with three tests that each wait 300ms, the run took 907ms with normal test, and 304ms after switching to test.concurrent.
This is useful for integration tests that wait on external APIs or databases when you want to shorten the test run time. Below, using results from my test environment, I go through when it speeds things up and when it does not, an example of tests that fail when run concurrently, and the option that runs a whole file concurrently.
The test environment is Ubuntu 24.04 (x86_64, 4 cores) with Bun 1.4.4. For type checking I used @types/bun 1.4.4 and TypeScript 5.9.3. All times are values measured in this test environment. I did not test --parallel, which runs files across multiple CPU cores, combinations with other modifiers such as test.concurrent.each, or Windows and macOS.
- 1. Changing test to test.concurrent makes the waiting overlap
- 2. A normal test in between makes the tests wait for it
- 3. Tests with no waiting do not get faster
- 4. Tests that use the same variable fail when run concurrently
- 5. Running a whole file at once with –concurrent, and keeping order with test.serial
- 6. The number of tests running at once is set by –max-concurrency
- 7. describe.concurrent, beforeEach, and afterEach
- 8. Running only some files concurrently with concurrentTestGlob
Changing test to test.concurrent makes the waiting overlap
To see when each test started and finished, I prepared two functions in timeline.ts. stamp prints the elapsed time in ms since timeline.ts was loaded, and fakeFetch waits for the specified time instead of waiting on an API response.
// Records the start and end of each test as elapsed time (ms) since the first test started
const t0 = performance.now();
export function stamp(name: string, event: "開始" | "終了") {
console.log(`${String(Math.round(performance.now() - t0)).padStart(4)}ms ${name} ${event}`);
}
// Instead of waiting on an external API, waits for the specified time
export const fakeFetch = (ms: number) => Bun.sleep(ms);
First, I write three tests with normal test, each waiting 300ms.
import { expect, test } from "bun:test";
import { fakeFetch, stamp } from "./timeline";
for (const name of ["ユーザー", "注文", "在庫"]) {
test(`${name}のAPIを待つ`, async () => {
stamp(name, "開始");
await fakeFetch(300);
stamp(name, "終了");
expect(true).toBe(true);
});
}
$ bun test sequential.test.ts
bun test v1.4.4 (38afee6c8)
sequential.test.ts:
0ms ユーザー 開始
301ms ユーザー 終了
(pass) ユーザーのAPIを待つ [300.90ms]
301ms 注文 開始
601ms 注文 終了
(pass) 注文のAPIを待つ [300.52ms]
602ms 在庫 開始
902ms 在庫 終了
(pass) 在庫のAPIを待つ [300.55ms]
3 pass
0 fail
3 expect() calls
Ran 3 tests across 1 file. [907.00ms]
As the official documentation on concurrency explains, bun test runs the tests in one file one after another unless you specify otherwise. Each test starts only after the previous one finishes, so the total was about 900ms.
Next, I change test to test.concurrent. I changed only one line.
for (const name of ["ユーザー", "注文", "在庫"]) {
test.concurrent(`${name}のAPIを待つ`, async () => {
// The body is the same
});
}
$ bun test concurrent.test.ts
bun test v1.4.4 (38afee6c8)
concurrent.test.ts:
0ms ユーザー 開始
0ms 注文 開始
0ms 在庫 開始
301ms ユーザー 終了
301ms 注文 終了
301ms 在庫 終了
(pass) ユーザーのAPIを待つ [300.83ms]
(pass) 注文のAPIを待つ [300.57ms]
(pass) 在庫のAPIを待つ [300.56ms]
3 pass
0 fail
3 expect() calls
Ran 3 tests across 1 file. [304.00ms]
All three tests started together, their waits overlapped, and the total became 304ms. The (pass) lines are printed together after all three finish. When I ran each file five times, the totals were 905–907ms for normal test and 304–311ms for test.concurrent.

As the official documentation for test.concurrent describes, tests written with test.concurrent run at the same time even without the --concurrent option.
A normal test in between makes the tests wait for it
The third row of the figure shows the result for a file in which one normal test sits between test.concurrent tests.
import { expect, test } from "bun:test";
import { fakeFetch, stamp } from "./timeline";
test.concurrent("A", async () => { stamp("A", "開始"); await fakeFetch(300); stamp("A", "終了"); });
test.concurrent("B", async () => { stamp("B", "開始"); await fakeFetch(300); stamp("B", "終了"); });
test("ふつうのテスト C", async () => { stamp("C", "開始"); await fakeFetch(300); stamp("C", "終了"); });
test.concurrent("D", async () => { stamp("D", "開始"); await fakeFetch(300); stamp("D", "終了"); });
test.concurrent("E", async () => { stamp("E", "開始"); await fakeFetch(300); stamp("E", "終了"); });
$ bun test mixed.test.ts
bun test v1.4.4 (38afee6c8)
mixed.test.ts:
1ms A 開始
1ms B 開始
301ms A 終了
301ms B 終了
(pass) A [300.99ms]
(pass) B [300.51ms]
301ms C 開始
602ms C 終了
(pass) ふつうのテスト C [300.69ms]
602ms D 開始
602ms E 開始
902ms D 終了
903ms E 終了
(pass) D [300.69ms]
(pass) E [300.52ms]
5 pass
0 fail
Ran 5 tests across 1 file. [906.00ms]
C did not start until A and B, which ran at the same time, had both finished. D and E also started only after C finished. Even though four of the five tests were test.concurrent, the single normal test in between split the run into three stages, and the total took 906ms. If you want tests to run together, write them consecutively without a normal test between them.
Tests with no waiting do not get faster
What test.concurrent overlaps is the time spent waiting on await. For tests that only compute and never wait, I saw no speedup. The following test keeps computing on the CPU for 300ms.
import { test } from "bun:test";
import { stamp } from "./timeline";
// CPU-only work with no waiting (keeps computing for 300ms)
function busy(ms: number) {
const end = performance.now() + ms;
let x = 0;
while (performance.now() < end) x++;
return x;
}
for (const name of ["計算1", "計算2", "計算3"]) {
test.concurrent(name, () => {
stamp(name, "開始");
busy(300);
stamp(name, "終了");
});
}
$ bun test cpu.test.ts
bun test v1.4.4 (38afee6c8)
cpu.test.ts:
0ms 計算1 開始
300ms 計算1 終了
(pass) 計算1 [300.31ms]
300ms 計算2 開始
600ms 計算2 終了
(pass) 計算2 [300.07ms]
600ms 計算3 開始
900ms 計算3 終了
(pass) 計算3 [300.06ms]
3 pass
0 fail
Ran 3 tests across 1 file. [905.00ms]
Even with test.concurrent, the tests ran one after another, and the total was 905ms. The test environment has four CPU cores, but the computation times did not overlap. The official documentation also lists test.concurrent as a way to speed up projects with many tests, in item 6 of the performance tips, and describes it as suited to tests that spend most of their time waiting (I/O-heavy tests). Running files across multiple CPU cores is a different option, --parallel, which is mentioned at the top of the concurrency section.
Tests that use the same variable fail when run concurrently
Some tests pass when run in order but fail when run concurrently. In the next two tests, both reset the variable cart, declared at the top of the file, and then use it.
import { expect, test } from "bun:test";
// Two tests use the same variable
let cart: string[] = [];
test.concurrent("りんごを入れる", async () => {
cart = [];
cart.push("りんご");
await Bun.sleep(50);
expect(cart).toEqual(["りんご"]);
});
test.concurrent("みかんを入れる", async () => {
cart = [];
cart.push("みかん");
await Bun.sleep(50);
expect(cart).toEqual(["みかん"]);
});
The paths in the test environment are replaced with /path/to/.
$ bun test shared.test.ts
bun test v1.4.4 (38afee6c8)
shared.test.ts:
5 |
6 | test.concurrent("りんごを入れる", async () => {
7 | cart = [];
8 | cart.push("りんご");
9 | await Bun.sleep(50);
10 | expect(cart).toEqual(["りんご"]);
^
error: expect(received).toEqual(expected)
[
- "りんご",
+ "みかん",
]
- Expected - 1
+ Received + 1
at <anonymous> (/path/to/api-app/shared.test.ts:10:16)
(fail) りんごを入れる [50.74ms]
(pass) みかんを入れる [50.49ms]
1 pass
1 fail
2 expect() calls
Ran 2 tests across 1 file. [53.00ms]
While the apple test was waiting in Bun.sleep(50), the mandarin test started, replaced cart with a new array, and added mandarin. When the apple test finished waiting, it saw an array containing only mandarin in place of its own data and failed.
I changed the code so that each test declares cart inside its own body, and both tests passed. I made the same change to the mandarin test.
test.concurrent("りんごを入れる", async () => {
const cart: string[] = [];
cart.push("りんご");
await Bun.sleep(50);
expect(cart).toEqual(["りんご"]);
});
(pass) りんごを入れる [50.40ms]
(pass) みかんを入れる [50.20ms]
2 pass
0 fail
If you make a test that rewrites something outside the test, such as a variable at the top of the file, a test.concurrent, it ends up reading the state after another test has rewritten it, as in the example above. Use test.concurrent for tests that each use their own data.
Running a whole file at once with –concurrent, and keeping order with test.serial
Instead of changing tests one by one to test.concurrent, running bun test --concurrent treats every test as test.concurrent (official documentation on –concurrent). For tests that must still run in order, write them with test.serial.
In the following file, the second test uses the result of the first (count). X and Y are unrelated to the others.
import { expect, test } from "bun:test";
import { fakeFetch, stamp } from "./timeline";
let count = 0;
test.serial("1つ目:数を1にする", async () => {
stamp("serial1", "開始"); await fakeFetch(200);
count = 1; expect(count).toBe(1);
stamp("serial1", "終了");
});
test.serial("2つ目:1つ目の結果を使う", async () => {
stamp("serial2", "開始"); await fakeFetch(200);
expect(count).toBe(1);
stamp("serial2", "終了");
});
test("ほかとは関係ないテスト X", async () => { stamp("X", "開始"); await fakeFetch(200); stamp("X", "終了"); });
test("ほかとは関係ないテスト Y", async () => { stamp("Y", "開始"); await fakeFetch(200); stamp("Y", "終了"); });
$ bun test --concurrent serial.test.ts
bun test v1.4.4 (38afee6c8)
serial.test.ts:
0ms serial1 開始
201ms serial1 終了
(pass) 1つ目:数を1にする [200.78ms]
201ms serial2 開始
401ms serial2 終了
(pass) 2つ目:1つ目の結果を使う [200.70ms]
402ms X 開始
402ms Y 開始
602ms X 終了
602ms Y 終了
(pass) ほかとは関係ないテスト X [200.56ms]
(pass) ほかとは関係ないテスト Y [200.40ms]
4 pass
0 fail
2 expect() calls
Ran 4 tests across 1 file. [606.00ms]
The two test.serial tests ran one after another, while the X and Y tests, written with normal test, ran at the same time. The total was 606ms. Without --concurrent it was 806ms, so the run was shorter by the 200ms in which X and Y overlapped.
The number of tests running at once is set by –max-concurrency
There is a limit on how many tests can run at the same time. bun test --help showed "Default is 20" for the --max-concurrency option. When I ran 25 test.concurrent tests that each wait 200ms, the first 20 started together at the beginning. The 21st started at 201ms, which is when the earlier tests finished.
1ms テスト20 開始
(pass) テスト1 [200.73ms]
201ms テスト21 開始
(pass) テスト2 [200.52ms]
201ms テスト22 開始
You can change the limit with --max-concurrency (official documentation on –max-concurrency). I ran a file with six normal test tests, each waiting 200ms, using --concurrent together with --max-concurrency 2, and they ran two at a time.
$ bun test --concurrent --max-concurrency 2 many.test.ts
bun test v1.4.4 (38afee6c8)
many.test.ts:
0ms テスト1 開始
0ms テスト2 開始
201ms テスト1 終了
201ms テスト2 終了
(pass) テスト1 [200.77ms]
201ms テスト3 開始
(pass) テスト2 [200.41ms]
201ms テスト4 開始
401ms テスト3 終了
401ms テスト4 終了
(pass) テスト3 [200.42ms]
401ms テスト5 開始
(pass) テスト4 [200.42ms]
401ms テスト6 開始
602ms テスト5 終了
602ms テスト6 終了
(pass) テスト5 [200.44ms]
(pass) テスト6 [200.42ms]
6 pass
0 fail
Ran 6 tests across 1 file. [605.00ms]
For the same file, the total time was 1208ms with no options, 204ms with --concurrent alone, and 605ms with --concurrent plus --max-concurrency 2. When you need to limit how many tests run at once, for example when the test database has a limited number of connections, set this value lower.
describe.concurrent, beforeEach, and afterEach
When tests are wrapped in describe.concurrent, they ran at the same time even when written with normal test.
import { describe, test } from "bun:test";
import { fakeFetch, stamp } from "./timeline";
describe.concurrent("APIのまとまり", () => {
test("ユーザー", async () => { stamp("ユーザー", "開始"); await fakeFetch(300); stamp("ユーザー", "終了"); });
test("注文", async () => { stamp("注文", "開始"); await fakeFetch(300); stamp("注文", "終了"); });
test("在庫", async () => { stamp("在庫", "開始"); await fakeFetch(300); stamp("在庫", "終了"); });
});
describe.test.ts:
0ms ユーザー 開始
0ms 注文 開始
1ms 在庫 開始
301ms ユーザー 終了
301ms 注文 終了
301ms 在庫 終了
(pass) APIのまとまり > ユーザー [300.83ms]
(pass) APIのまとまり > 注文 [300.52ms]
(pass) APIのまとまり > 在庫 [300.46ms]
beforeEach and afterEach ran once per test, even for tests that run at the same time. I checked this with the following file.
import { afterEach, beforeEach, test } from "bun:test";
import { fakeFetch, stamp } from "./timeline";
beforeEach(() => stamp("beforeEach", "開始"));
afterEach(() => stamp("afterEach", "開始"));
test.concurrent("A", async () => { stamp("A", "開始"); await fakeFetch(200); stamp("A", "終了"); });
test.concurrent("B", async () => { stamp("B", "開始"); await fakeFetch(200); stamp("B", "終了"); });
beforeEach printed once before A and once before B, and afterEach printed twice as well.
0ms beforeEach 開始
0ms A 開始
0ms beforeEach 開始
0ms B 開始
201ms A 終了
201ms B 終了
201ms afterEach 開始
(pass) A [200.78ms]
201ms afterEach 開始
(pass) B [200.46ms]
The official documentation on lifecycle hooks says that onTestFinished, which registers cleanup to run after a test finishes, does not support tests that run concurrently, and recommends using test.serial instead (lifecycle documentation).
Running only some files concurrently with concurrentTestGlob
If you want to run only integration test files concurrently and keep unit tests in order, write a file name pattern in concurrentTestGlob in bunfig.toml. Files that match the pattern are handled the same way as when --concurrent is passed.
[test]
concurrentTestGlob = "**/*.integration.test.ts"
I placed a file with the same contents as sequential.test.ts under two names, api.integration.test.ts and unit.test.ts, and ran bun test.
$ bun test
bun test v1.4.4 (38afee6c8)
api.integration.test.ts:
0ms ユーザー 開始
0ms 注文 開始
0ms 在庫 開始
301ms ユーザー 終了
301ms 注文 終了
301ms 在庫 終了
(pass) ユーザーのAPIを待つ [300.93ms]
(pass) 注文のAPIを待つ [300.67ms]
(pass) 在庫のAPIを待つ [300.65ms]
unit.test.ts:
302ms ユーザー 開始
602ms ユーザー 終了
(pass) ユーザーのAPIを待つ [300.53ms]
602ms 注文 開始
903ms 注文 終了
(pass) 注文のAPIを待つ [300.53ms]
903ms 在庫 開始
1204ms 在庫 終了
(pass) 在庫のAPIを待つ [301.45ms]
6 pass
0 fail
6 expect() calls
Ran 6 tests across 2 files. [1208.00ms]
The three tests in api.integration.test.ts, which match the pattern, ran at the same time, and the three in unit.test.ts ran one at a time. Without changing the file contents, you can decide per file by its name whether tests run concurrently. The official documentation says that when you run with --concurrent, tests in all files become eligible to run concurrently regardless of this setting.





