日本語

How to Find Flaky Tests in Bun with –rerun-each

​​Bun​Testing,TypeScript

Running bun test --rerun-each 100 executes the test file 100 times in a row and counts how many runs failed. A flaky test can pass on a single run, but if you run it dozens of times, failures show up.

This is the method I use when I want to find the cause of a test that fails only sometimes in CI and never reproduces locally (such tests are also called flaky tests). I show two examples: a test that fails because of random numbers, and a test that fails because of state left over from an earlier run. The output shown is what I got when running them in my test environment.

Note

The test environment was Ubuntu 24.04 (x86_64) with Bun 1.4.4. The timings are from runs in this environment. I did not test running multiple test files together, combining with --randomize, Windows, or macOS. Paths of the test environment in the output are replaced with /path/to/.

Write a test that fails about one run in ten

As an example, I wrote a function makeCode in code.ts that creates a 6-digit confirmation code. It converts a random number from 0 to 999999 into a string and returns it.

ts
// Create a 6-digit confirmation code
export function makeCode(): string {
  return String(Math.floor(Math.random() * 1_000_000));
}

In the test file code.test.ts, I check that the returned string is a 6-digit number.

ts
import { expect, test } from "bun:test";
import { makeCode } from "./code";

test("確認コードは6桁の数字", () => {
  expect(makeCode()).toMatch(/^\d{6}$/);
});

This function has a bug: when the random number is less than 100000, the result has 5 digits or fewer. Math.random() returns a number from 0 (inclusive) to 1 (exclusive), so the bug happens about one run in ten. I ran bun test ten times and collected only the pass and fail lines of each result. All ten runs showed 1 pass 0 fail. When you run a test once at a time, a bug like this is easy to miss.

Run it 100 times in a row with –rerun-each

The official documentation's "Rerun tests" section says that --rerun-each is meant to make it easier to find failures in flaky tests, or tests whose results are not the same every time. The description in bun test --help is "Re-run each test file times".

text
$ bun test --rerun-each 100
bun test v1.4.4 (38afee6c8)

code.test.ts: (run #1)
(pass) 確認コードは6桁の数字 [0.26ms]

code.test.ts: (run #2)
(pass) 確認コードは6桁の数字 [0.01ms]

code.test.ts: (run #3)
1 | import { expect, test } from "bun:test";
2 | import { makeCode } from "./code";
3 | 
4 | test("確認コードは6桁の数字", () => {
5 |   expect(makeCode()).toMatch(/^\d{6}$/);
                         ^
error: expect(received).toMatch(expected)

Expected substring or pattern: /^\d{6}$/
Received: "18621"

      at <anonymous> (/path/to/code-app/code.test.ts:5:22)
(fail) 確認コードは6桁の数字 [0.25ms]

(run #4 から run #100 までは略)

7 tests failed:
(fail) 確認コードは6桁の数字 [0.25ms]
(fail) 確認コードは6桁の数字 [0.07ms]
(fail) 確認コードは6桁の数字 [0.04ms]
(fail) 確認コードは6桁の数字 [0.02ms]
(fail) 確認コードは6桁の数字 [0.04ms]
(fail) 確認コードは6桁の数字 [0.04ms]
(fail) 確認コードは6桁の数字 [0.05ms]

 93 pass
 7 fail
 100 expect() calls
Ran 100 tests across 1 file. [16.00ms]

Each run gets a heading like code.test.ts: (run #3), so you can see which run failed. In this run, runs 3, 5, 40, 42, 48, 73, and 93 failed. The final 7 tests failed: list shows only the test names, so to find which run failed, look at the headings above.

The number in Ran 100 tests is the number of tests (1) multiplied by the number of runs (100). Even 100 runs finished in 16 ms.

I ran the same command five more times and compared the failure counts.

Failure counts from five runs of --rerun-each 100: 8, 10, 10, 9, and 6
Failures out of 100 runs in each of five executions of bun test –rerun-each 100

Every run had between 6 and 10 failures, and no run had zero failures. When I ran bun test ten times, one at a time, this test never failed. When run 100 times in a row, every execution produced some failures.

Extract only the values from the failing runs

When there are many failures, the output gets long. In my test environment, bun test --rerun-each 3 with all runs passing printed one line to standard output and 14 lines to standard error. The test result lines went to standard error. So I added 2>&1 and used grep to pull out only the lines I needed. Extracting the Received lines lists the values that were returned in the failing runs.

text
$ bun test --rerun-each 100 2>&1 | grep -E '^Received'
Received: "8909"
Received: "10372"
Received: "40550"
Received: "63372"
Received: "78671"
Received: "71803"
Received: "3295"
Received: "82063"
Received: "46725"
Received: "17798"

All of them are 5 digits or fewer. This shows the cause: when the random number is small, there are not enough digits.

Stop at the first failure

If you only want to find the cause, one failure is enough. Adding --bail, which is also in the official documentation, stopped the run at the first failure.

text
$ bun test --rerun-each 100 --bail
bun test v1.4.4 (38afee6c8)

code.test.ts: (run #1)
(pass) 確認コードは6桁の数字 [0.13ms]

code.test.ts: (run #2)
(pass) 確認コードは6桁の数字

code.test.ts: (run #3)
(コードの抜き出しは略)
error: expect(received).toMatch(expected)

Expected substring or pattern: /^\d{6}$/
Received: "3812"

      at <anonymous> (/path/to/code-app/code.test.ts:5:22)
(fail) 確認コードは6桁の数字 [0.15ms]
Ran 3 tests across 1 file. [3.00ms]
Bailed out after 1 failure

After the fix, confirm that the failures are gone

I fixed the function with padStart, which pads the missing digits at the start with 0.

ts
// Create a 6-digit confirmation code (pad missing digits with 0)
export function makeCode(): string {
  return String(Math.floor(Math.random() * 1_000_000)).padStart(6, "0");
}

After the fix, I increased the count to --rerun-each 10000 to check. All 10,000 runs passed, and the run took 452 ms.

text
$ bun test --rerun-each 10000 2>&1 | tail -5

 10000 pass
 0 fail
 10000 expect() calls
Ran 10000 tests across 1 file. [452.00ms]
Point

After fixing a flaky test, run --rerun-each with a higher count than you used before, and confirm that you get 0 fail. Passing once does not tell you whether the test is fixed.

A test that fails from its second run has state left over from the earlier run

--rerun-each finds more than tests whose results change randomly. It also finds tests that fail from the second run on because of state left over from an earlier run.

I prepared cart.ts, which keeps the cart items in an array inside the module. It uses console.log to report when the module is loaded.

ts
// Keep the cart items inside the module
const items: string[] = [];
console.log("cart.ts を読み込んだ");

export function add(name: string): number {
  items.push(name);
  return items.length;
}

In the test file cart.test.ts, I check that adding one item gives a cart with one item. This file also uses console.log, so I can see when it runs.

ts
import { expect, test } from "bun:test";
import { add } from "./cart";

console.log("cart.test.ts を実行した");

test("1つ入れると、カートは1件になる", () => {
  expect(add("りんご")).toBe(1);
});

Running bun test once passes. With --rerun-each 3, only the first run passed, and the second and third runs failed.

text
$ bun test --rerun-each 3
bun test v1.4.4 (38afee6c8)

cart.test.ts: (run #1)
cart.ts を読み込んだ
cart.test.ts を実行した
(pass) 1つ入れると、カートは1件になる [0.04ms]

cart.test.ts: (run #2)
cart.test.ts を実行した
(コードの抜き出しは略)
error: expect(received).toBe(expected)

Expected: 1
Received: 2

      at <anonymous> (/path/to/cart-app/cart.test.ts:7:22)
(fail) 1つ入れると、カートは1件になる [0.12ms]

cart.test.ts: (run #3)
cart.test.ts を実行した
(コードの抜き出しは略)
error: expect(received).toBe(expected)

Expected: 1
Received: 3

      at <anonymous> (/path/to/cart-app/cart.test.ts:7:22)
(fail) 1つ入れると、カートは1件になる [0.03ms]

 1 pass
 2 fail
 3 expect() calls
Ran 3 tests across 1 file. [3.00ms]

In the console.log lines, cart.test.ts を実行した appears every time, but cart.ts を読み込んだ appears only in the first run. In this check, the test file ran from the top on every run, but the cart.ts it had loaded was not loaded again. So the apple item added in the first run stayed in the array, and Received grew to 2 and then 3.

The failure happened because state that was not cleaned up between tests stayed in the module. So I added a clear function that empties the items to cart.ts, and called it in beforeEach (a hook that runs before each test) in the test file.

ts
// Function added to cart.ts
export function clear(): void {
  items.length = 0;
}
ts
import { beforeEach, expect, test } from "bun:test";
import { add, clear } from "./cart";

console.log("cart.test.ts を実行した");

beforeEach(() => {
  clear();
});

test("1つ入れると、カートは1件になる", () => {
  expect(add("りんご")).toBe(1);
});

When I ran --rerun-each 3 again, all three runs passed.

text
$ bun test --rerun-each 3
bun test v1.4.4 (38afee6c8)

cart.test.ts: (run #1)
cart.ts を読み込んだ
cart.test.ts を実行した
(pass) 1つ入れると、カートは1件になる [0.09ms]

cart.test.ts: (run #2)
cart.test.ts を実行した
(pass) 1つ入れると、カートは1件になる [0.01ms]

cart.test.ts: (run #3)
cart.test.ts を実行した
(pass) 1つ入れると、カートは1件になる

 3 pass
 0 fail
 3 expect() calls
Ran 3 tests across 1 file. [3.00ms]

With retry, failures cannot be counted

bun test also has --retry, which runs a failed test again. The official documentation says that a test that fails and then passes on a retry is counted as passed. This is the opposite of what you want when looking for flaky tests.

Using both options together on the command line stopped with an error.

text
$ bun test --rerun-each 10 --retry 3
error: --retry cannot be used with --rerun-each

However, retry written per test as { retry: 3 } did work together with --rerun-each. I used the makeCode from before the fix and wrote a test code-retry.test.ts with retry set.

ts
import { expect, test } from "bun:test";
import { makeCode } from "./code";

test("確認コードは6桁の数字(retry つき)", { retry: 3 }, () => {
  expect(makeCode()).toMatch(/^\d{6}$/);
});

Running it with --rerun-each 100 gave 0 fail. In 14 of the 100 runs, the output showed (attempt 2), which means the first attempt failed and the second passed.

text
$ bun test code-retry.test.ts --rerun-each 100 2>&1 | grep -E '^\(pass\).*attempt|^ *[0-9]+ (pass|fail)$|^Ran'
(pass) 確認コードは6桁の数字(retry つき) (attempt 2)
(同じ行が全部で14行)
 100 pass
 0 fail
Ran 100 tests across 1 file. [9.00ms]

If you only look at the fail count, this test looks stable. When you investigate a test that uses retry, also check for lines like (attempt 2), which show that it passed on a later attempt.

To always use –rerun-each, write it in bunfig.toml

The official configuration documentation describes how to write rerunEach in the [test] section of bunfig.toml. I wrote rerunEach = 3 and ran bun test with no options. It ran three times.

toml
[test]
rerunEach = 3
text
$ bun test
bun test v1.4.4 (38afee6c8)

code.test.ts: (run #1)
(pass) 確認コードは6桁の数字 [0.13ms]

code.test.ts: (run #2)
(pass) 確認コードは6桁の数字 [0.01ms]

code.test.ts: (run #3)
(pass) 確認コードは6桁の数字

 3 pass
 0 fail
 3 expect() calls
Ran 3 tests across 1 file. [3.00ms]

If you write it in bunfig.toml, a plain bun test run with no options also repeats the tests this many times each time.

​Posted by 86