日本語

Setting Test and Install Options in Bun’s bunfig.toml

​​Bun​npm,package management,Testing,TypeScript

Adding [test] and [install] sections to bunfig.toml lets you keep in the project the options you had been passing to bun test and bun add each time. You can set the folders where tests are searched, the coverage thresholds, the format of version ranges written to package.json, and more, all in one file.

These settings are useful when you want tests to fail in CI if coverage is too low, when you want to avoid installing a package that was just published as a precaution against supply chain attacks, or when you want to stop installs that would leave the lockfile out of date.

Note

The test environment was Ubuntu 24.04 (x86_64) with Bun 1.4.4. I installed packages from the standard npm registry (registry.npmjs.org) on November 22, 2026. I did not test settings in bunfig.toml other than the ones covered here (such as linker, registry, and scopes), the config file placed under XDG_CONFIG_HOME, specifying a different file with --config, or Windows and macOS. I replaced the host name in the JUnit XML output with (hostname).

Where to put bunfig.toml and what it applies to

The official bunfig.toml documentation says that bunfig.toml should be placed in the project root (the same directory as package.json). You can also place one at ~/.bunfig.toml in your home directory. However, only package management commands such as bun install and bun add read the home file. When both exist, the project's value is used for the same setting.

In the test environment, I wrote the following two settings in ~/.bunfig.toml. I did not place a bunfig.toml in the project.

toml
[install]
exact = true

[test]
coverage = true

When I ran bun add is-odd, package.json got "is-odd": "3.0.1" without a ^. The [install] setting from the home file was applied. On the other hand, bun test did not print a coverage table. The [test] setting in the home file was not used by bun test.

text
$ bun test
bun test v1.4.4 (38afee6c8)

tests/price.test.ts:
(pass) 税込み価格 [0.15ms]

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

Next, I placed a bunfig.toml containing only exact = false in the project and ran bun add is-number. This time it was installed as "is-number": "^7.0.0" with a ^, so the project's value was applied. Then I ran bun add -E is-even with -E (--exact), and it was written as "is-even": "1.0.0" without a ^.

json
{
  "name": "global-app",
  "private": true,
  "dependencies": {
    "is-even": "1.0.0",
    "is-number": "^7.0.0",
    "is-odd": "3.0.1"
  }
}

Test settings must be written in the project's bunfig.toml, not in the home one.

Put test settings under [test]

Here I tried these settings on a small project like the following. src/price.ts has two functions: withTax, which calculates the price including tax, and discount, which calculates the price after a discount. I wrote a test in tests/price.test.ts covering only withTax.

text
$ find . -type f | sort
./package.json
./src/price.ts
./src/scratch.test.ts
./tests/price.test.ts
./vendor/lib/broken.test.ts

I assumed vendor/lib/broken.test.ts is a test for code taken from elsewhere and src/scratch.test.ts is a test I was still writing. Both are deliberately set to fail. When I ran bun test without a bunfig.toml, all three files were found, with 1 pass and 2 fail.

Decide where tests are searched for

If you want to exclude the tests inside vendor, write a glob pattern in pathIgnorePatterns.

toml
[test]
pathIgnorePatterns = ["vendor/**"]
text
$ bun test
bun test v1.4.4 (38afee6c8)

src/scratch.test.ts:
(失敗の内容は略)
(fail) src に置いた書きかけのテスト [0.21ms]

tests/price.test.ts:
(pass) 税込み価格 [0.09ms]

 1 pass
 1 fail
 2 expect() calls
Ran 2 tests across 2 files. [4.00ms]

The tests in vendor were excluded, leaving two files. When I then ran bun test --path-ignore-patterns='tests/**', the tests in vendor ran and the tests in tests were excluded. A value given on the command line replaces the value in bunfig.toml; it is not added to it. The official documentation describes the same behavior.

If you know which folder holds your tests, you can search only that folder with root.

toml
[test]
root = "./tests"
text
$ bun test
bun test v1.4.4 (38afee6c8)

tests/price.test.ts:
(pass) 税込み価格 [0.10ms]

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

Neither the tests in src nor those in vendor were searched, and only the one test in tests ran. In the examples that follow, I deleted src/scratch.test.ts and vendor, and kept root = "./tests".

Fail when coverage is too low

Setting coverage to true prints the coverage table without needing --coverage. If you write a ratio in coverageThreshold, bun test exits with a non-zero code when coverage falls below it. Adding "lcov" to coverageReporter also writes a file that CI services and similar tools can read.

toml
[test]
root = "./tests"
coverage = true
coverageThreshold = { lines = 0.9, functions = 0.9 }
coverageReporter = ["text", "lcov"]
text
$ bun test
bun test v1.4.4 (38afee6c8)

tests/price.test.ts:
(pass) 税込み価格 [0.10ms]
--------------|---------|---------|-------------------
File          | % Funcs | % Lines | Uncovered Line #s
--------------|---------|---------|-------------------
All files     |   50.00 |   33.33 |
 src/price.ts |   50.00 |   33.33 | 7-9,11
--------------|---------|---------|-------------------

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

No tests failed, but functions were at 50% and lines at 33.33%, below the 90% threshold, so the exit code was 1. The output does not print a message saying the threshold was not met. If CI fails even though all tests pass, check the percentages in the table.

A lcov.info file was created in the coverage folder. You can change the output location with coverageDir (official documentation for coverageDir).

When I added two tests for discount in tests/discount.test.ts, the table showed 100.00 for both functions and lines, and the exit code was 0.

Automatically retry failed tests

Writing retry reruns a failed test up to that many times. I tested with a test that fails for the first two runs and passes on the third.

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

// A test that fails the first two times and passes on the third
let calls = 0;

test("3回目に通る", () => {
  calls++;
  console.log(`実行 ${calls} 回目`);
  expect(calls).toBeGreaterThan(2);
});

Without retry in bunfig.toml, the test was marked (fail) on the first run. With retry = 2, it passed on the third run.

text
$ cat bunfig.toml
[test]
root = "./tests"
retry = 2
$ bun test tests/flaky.test.ts
(1回目と2回目の失敗の内容は略)
実行 3 回目
(pass) 3回目に通る (attempt 3)

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

The official documentation says that passing --retry on the command line overrides the value in bunfig.toml. In the test environment with Bun 1.4.4, that did not happen.

retry in bunfig.toml Option given Runs Result
none --retry=1 2 fail
none --retry=2 3 pass
2 --retry=0 3 pass
2 --retry=1 3 pass
1 none 2 fail
1 --retry=3 2 fail

While retry was set in bunfig.toml, the test was retried according to the bunfig.toml value no matter what --retry was set to. Placing --retry before or after the file name made no difference. Without a bunfig.toml, --retry worked as specified.

Warning

If you want to run a test only once, with no retries, to investigate a flaky test, do not rely on --retry=0. Instead, remove retry from bunfig.toml first, then run the test.

Write JUnit XML output for CI

Writing a file name for junit under [test.reporter] writes JUnit-format XML in addition to the usual output. This means you don't need --reporter=junit --reporter-outfile=… on the command line.

toml
[test]
root = "./tests"

[test.reporter]
junit = "test-results.xml"

This is test-results.xml after running bun test (the time values change on each run).

xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="bun test" tests="3" assertions="3" failures="0" skipped="0" time="0.003825176">
  <testsuite name="tests/discount.test.ts" file="tests/discount.test.ts" tests="2" assertions="2" failures="0" skipped="0" time="0.001304969" hostname="(ホスト名)">
    <testcase name="2割引き" classname="" time="0.000141" file="tests/discount.test.ts" line="4" assertions="1" />
    <testcase name="割引率が範囲の外ならエラー" classname="" time="0.00008" file="tests/discount.test.ts" line="8" assertions="1" />
  </testsuite>
  <testsuite name="tests/price.test.ts" file="tests/price.test.ts" tests="1" assertions="1" failures="0" skipped="0" time="0.000208074" hostname="(ホスト名)">
    <testcase name="税込み価格" classname="" time="0.000026" file="tests/price.test.ts" line="4" assertions="1" />
  </testsuite>
</testsuites>

There is one testsuite per test file and one testcase per test, with the test name and the line number in the file.

Put install settings under [install]

Write versions without ^ in package.json

Normally bun add writes a range such as ^3.0.1 to package.json. Setting exact to true writes the exact version that was installed, so you don't need -E on every bun add.

toml
[install]
exact = true

These are the results of running bun add is-odd without a bunfig.toml, and then bun add is-number after placing this bunfig.toml.

json
{
  "name": "app",
  "private": true,
  "dependencies": {
    "is-number": "7.0.0",
    "is-odd": "^3.0.1"
  }
}

Keep recently published versions out of installs

Giving minimumReleaseAge a number of seconds excludes versions from install candidates if they were published less than that long ago. The official bun install documentation presents this as a precaution against supply chain attacks, where a malicious package is published and installed right away.

On the day I tested (November 22, 2026), the latest version of @types/node was 26.6.6, published on October 28. The version before it, 26.6.5, was published on October 9.

text
latest: 26.6.6
26.6.4 2026-10-01T22:39:22.769Z
26.6.5 2026-10-09T18:46:47.657Z
26.6.6 2026-10-28T18:36:36.455Z

I set 30 days (2,592,000 seconds) and ran bun add @types/node.

toml
[install]
minimumReleaseAge = 2592000
text
$ bun add @types/node
bun add v1.4.4 (38afee6c8)
Saved lockfile

installed @types/node@26.6.5

2 packages installed [3.00ms]

26.6.6, which was about 24 days old, was excluded, and 26.6.5, about 43 days old, was installed. package.json has "@types/node": "^26.6.5". When I removed bunfig.toml and reinstalled, 26.6.6 was installed. If you want to exempt specific packages from this rule, list their names in minimumReleaseAgeExcludes.

Stop installs that do not match the lockfile

Setting frozenLockfile to true stops bun install from rewriting bun.lock. If package.json and bun.lock do not match, the install stops with an error.

toml
[install]
frozenLockfile = true

After creating bun.lock, I manually added "is-number": "7.0.0" to dependencies in package.json and ran bun install.

text
$ bun install
bun install v1.4.4 (38afee6c8)
error: lockfile had changes, but lockfile is frozen
note: dependencies in package.json changed since bun.lock was saved: is-number was added
note: try re-running without --frozen-lockfile and commit the updated lockfile

The exit code was 1, and the output also showed which package did not match. While this setting was in effect, bun add is-even also stopped with the same error, and package.json was not changed. To add new packages, you need to remove frozenLockfile.

Summary of the settings I tried

Section Setting What it does Command-line equivalent
[test] root Folder where tests are searched for Not listed in --help
[test] pathIgnorePatterns Places where tests are not searched for --path-ignore-patterns (replaces the value)
[test] coverage Prints the coverage table --coverage
[test] coverageThreshold Exit code 1 if not met Not listed in --help
[test] coverageReporter Also writes an lcov file --coverage-reporter
[test] retry Retries failed tests --retry (did not take effect in 1.4.4)
[test.reporter] junit Writes JUnit XML --reporter=junit --reporter-outfile
[install] exact Writes versions without ^ -E, --exact
[install] minimumReleaseAge Excludes versions that are too new --minimum-release-age
[install] frozenLockfile Does not rewrite bun.lock --frozen-lockfile

The command-line names are those shown in bun test --help and bun install --help. For the home ~/.bunfig.toml, exact under [install] took effect, but coverage under [test] did not.

​Posted by 86