Files
bun.sh/docs/snippets/cli/test.mdx
Alistair Smith 099b5e430c feat(test): add --retry flag and emit separate testcase entries for retries in JUnit XML (#26866)
#### (Copies commits from #26447)

## Summary
- Add a global `--retry <N>` flag to `bun test` that sets a default
retry count for all tests (overridable by per-test `{ retry: N }`). Also
configurable via `[test] retry = N` in bunfig.toml.
- When a test passes after one or more retries, the JUnit XML reporter
emits a separate `<testcase>` entry for each failed attempt (with
`<failure>`), followed by the final passing `<testcase>`. This gives
flaky test detection tools per-attempt timing and result data using
standard JUnit XML that all CI systems can parse.

## Test plan
- `bun bd test test/js/junit-reporter/junit.test.js` — verifies separate
`<testcase>` entries appear in JUnit XML for tests that pass after retry
- `bun bd test test/cli/test/retry-flag.test.ts` — verifies the
`--retry` CLI flag applies a default retry count to all tests

## Changelog
<!-- CHANGELOG:START -->
- Added `--retry <N>` flag to `bun test` to set a default retry count
for all tests
- Added `[test] retry` option to bunfig.toml
- JUnit XML reporter now emits separate `<testcase>` entries for each
retry attempt, providing CI visibility into flaky tests
<!-- CHANGELOG:END -->

---------

Co-authored-by: Chris Lloyd <chrislloyd@anthropic.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-10 10:58:21 -08:00

106 lines
2.8 KiB
Plaintext

# CLI Usage
```bash
bun test <patterns>
```
### Execution Control
<ParamField path="--timeout" type="number" default="5000">
Set the per-test timeout in milliseconds (default 5000)
</ParamField>
<ParamField path="--rerun-each" type="number">
Re-run each test file <code>NUMBER</code> times, helps catch certain bugs
</ParamField>
<ParamField path="--retry" type="number">
Default retry count for all tests. Failed tests will be retried up to <code>NUMBER</code> times. Overridden by
per-test <code>{`{ retry: N }`}</code>
</ParamField>
<ParamField path="--concurrent" type="boolean">
Treat all tests as <code>test.concurrent()</code> tests
</ParamField>
<ParamField path="--randomize" type="boolean">
Run tests in random order
</ParamField>
<ParamField path="--seed" type="number">
Set the random seed for test randomization
</ParamField>
<ParamField path="--bail" type="number" default="1">
Exit the test suite after <code>NUMBER</code> failures. If you do not specify a number, it defaults to 1.
</ParamField>
<ParamField path="--max-concurrency" type="number" default="20">
Maximum number of concurrent tests to execute at once (default 20)
</ParamField>
### Test Filtering
<ParamField path="--todo" type="boolean">
Include tests that are marked with <code>test.todo()</code>
</ParamField>
<ParamField path="--test-name-pattern" type="string">
Run only tests with a name that matches the given regex. Alias: <code>-t</code>
</ParamField>
### Reporting
<ParamField path="--reporter" type="string">
Test output reporter format. Available: <code>junit</code> (requires --reporter-outfile), <code>dots</code>. Default:
console output.
</ParamField>
<ParamField path="--reporter-outfile" type="string">
Output file path for the reporter format (required with --reporter)
</ParamField>
<ParamField path="--dots" type="boolean">
Enable dots reporter. Shorthand for --reporter=dots
</ParamField>
### Coverage
<ParamField path="--coverage" type="boolean">
Generate a coverage profile
</ParamField>
<ParamField path="--coverage-reporter" type="string" default="text">
Report coverage in <code>text</code> and/or <code>lcov</code>. Defaults to <code>text</code>
</ParamField>
<ParamField path="--coverage-dir" type="string" default="coverage">
Directory for coverage files. Defaults to <code>coverage</code>
</ParamField>
### Snapshots
<ParamField path="--update-snapshots" type="boolean">
Update snapshot files. Alias: <code>-u</code>
</ParamField>
## Examples
Run all test files:
```bash terminal icon="terminal"
bun test
```
Run all test files with "foo" or "bar" in the file name:
```bash terminal icon="terminal"
bun test foo bar
```
Run all test files, only including tests whose names includes "baz":
```bash terminal icon="terminal"
bun test --test-name-pattern baz
```