网页
综合
更新时间 2026-11-16 06:55:43
Test runner | Bun Docs
自动蜘蛛
bun.sh
正文约 4606 字
Bun's fast, built-in, Jest-compatible test runner with TypeScript support, lifecycle hooks, mocking, and watch mode
Bun ships with a fast, built-in, Jest-compatible test runner. Tests run in the Bun runtime and support the following features.
Bun aims for compatibility with Jest, but not everything is implemented. To track compatibility, see this tracking issue.
You write tests in JavaScript or TypeScript with a Jest-like API. See Writing tests.
The runner recursively searches the working directory for files that match the following patterns:
To filter the set of test files to run, pass additional positional arguments to bun test. Any test file with a path that matches one of the filters runs. Filters are commonly file or directory names; glob patterns are not yet supported.
To filter by test name, use the -t/--test-name-pattern flag.
To run a specific file in the test runner, make sure the path starts with ./ or / to distinguish it from a filter name.
By default the test runner runs all tests in a single process: it loads all --preload scripts (see Lifecycle), then runs every file in one shared global. Pass --parallel to spread files across CPU cores instead. If a test fails, the test runner exits with a non-zero exit code.
bun test supports a variety of CI/CD integrations.
bun test automatically detects when it's running inside GitHub Actions and emits GitHub Actions annotations to the console directly.
No configuration is needed, other than installing bun in the workflow and running bun test.
To use bun test in a GitHub Actions workflow, add the following step:
To type check the tests in the same step, run bun test --check. See Type check your tests.
To write a JUnit XML report, pass --reporter=junit together with --reporter-outfile.
bun test still writes to stdout/stderr as usual, and writes the JUnit XML report to the given path at the end of the run.
JUnit XML is a popular format for reporting test results in CI/CD pipelines.
Use the --timeout flag to specify a per-test timeout in milliseconds. If a test times out, Bun marks it as failed. The default value is 5000.
To run test files across CPU cores, see --parallel. The flags below control concurrency of tests within a file.
By default, Bun runs all tests sequentially within each test file. Concurrent execution runs async tests in parallel, which speeds up test suites with independent tests.
Use the --concurrent flag to run all tests concurrently within their respective files:
When this flag is enabled, all tests run in parallel unless marked with test.serial.
Control the maximum number of tests running simultaneously with the --max-concurrency flag:
The limit helps prevent resource exhaustion when running many concurrent tests. The default value is 20.
Mark individual tests to run concurrently, even when the --concurrent flag is not used:
Force tests to run sequentially, even when the --concurrent flag is enabled:
test.sequential is an alias for test.serial, for Vitest compatibility.
Use the --retry flag to automatically retry failed tests up to a given number of times. If a test fails and then passes on a subsequent attempt, Bun reports it as passing.
Per-test { retry: N } overrides the global --retry value:
You can also set this in bunfig.toml:
Use the --rerun-each flag to run each test multiple times. This surfaces flaky or non-deterministic test failures.
Use the --randomize flag to run tests in a random order. This helps detect tests that depend on shared state or execution order.
With --randomize, Bun displays the seed used for randomization in the test summary:
Use the --seed flag to specify the randomization seed and reproduce the same test order when debugging order-dependent failures.
The --seed flag implies --randomize, so you don't need to specify both. The same seed always produces the same test execution order.
describe.shuffle() runs the tests in one block, including nested blocks, in random order. You don't need --randomize. The summary prints the seed, and running with that --seed reproduces the block's order.
Use the --bail flag to abort the test run after a given number of test failures. By default, Bun runs all tests and reports all failures, but in CI it can be preferable to stop early and reduce CPU usage.
Like bun run, bun test accepts the --watch flag to watch for changes and re-run tests.
bun test runs TypeScript tests without checking their types. Use the --check flag to type check the test files, and everything they import, before any test runs.
If there is a type error, Bun prints it, exits with code 1, and runs no tests. See bun check.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。