Skip to main content
bun test is deeply integrated with Bun’s runtime. This integration is part of what makes bun test fast.

Environment Variables

NODE_ENV

bun test sets $NODE_ENV to "test" unless it’s already set in the environment or in .env files. Most test runners do the same.
test.ts
You can override this by setting NODE_ENV explicitly:
terminal

TZ (Timezone)

bun test uses UTC (Etc/UTC) as the time zone unless the TZ environment variable overrides it. This keeps date and time behavior consistent across machines.
test.ts
To test with a specific time zone:
terminal

Test Timeouts

Each test has a default timeout of 5000ms (5 seconds). Tests that exceed it fail.

Global Timeout

Change the timeout globally with the --timeout flag:
terminal

Per-Test Timeout

Set a per-test timeout as the third argument to the test function:
test.ts

Infinite Timeout

Use 0 or Infinity to disable the timeout:
test.ts

Error Handling

Unhandled Errors

bun test tracks unhandled promise rejections and errors that occur between tests. If any occur, the final exit code is non-zero (the count of such errors), even if all tests pass. This helps catch errors in asynchronous code that might otherwise go unnoticed:
test.ts

Promise Rejections

Unhandled promise rejections are also caught:
test.ts

Custom Error Handling

You can set up custom error handlers in your test setup:
test-setup.ts

CLI Flags Integration

Several Bun CLI flags also work with bun test:

Memory Usage

terminal

Debugging

terminal

Module Loading

terminal

Watch and Hot Reloading

Watch Mode

With the --watch flag, the test runner watches for file changes and re-runs affected tests.
terminal
Only affected tests re-run:
math.test.ts
If you modify math.js, only math.test.ts re-runs, not all tests.

Hot Reloading

The --hot flag is similar, but more aggressive about preserving state between runs:
terminal
For most tests, use --watch: it gives better isolation between runs.

Global Variables

The following globals are available in test files without importing:
test.ts
You can also import them explicitly:
test.ts

Process Integration

Exit Codes

bun test uses standard exit codes:
  • 0: All tests passed, no unhandled errors
  • 1: Test failures occurred
  • >1: Number of unhandled errors (even if tests passed)

Signal Handling

The test runner handles common signals:
terminal

Environment Detection

Bun automatically detects certain environments and adjusts behavior:
test.ts

Performance Considerations

Single Process

The test runner runs all tests in a single process by default. This provides:
  • Faster startup - No need to spawn multiple processes
  • Shared memory - Efficient resource usage
  • Simple debugging - All tests in one process
However, this means:
  • Tests share global state (use lifecycle hooks to clean up)
  • One test crash can affect others
  • No true parallelization of individual tests

Memory Management

terminal

Test Isolation

Since tests run in the same process, ensure proper cleanup:
test.ts