> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 Bun 测试运行器设置代码覆盖率阈值

Bun 的测试运行器通过 `--coverage` 标志支持内置的代码覆盖率报告。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun test --coverage
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
test.test.ts:
✓ math > add [0.71ms]
✓ math > multiply [0.03ms]
✓ random [0.13ms]
-------------|---------|---------|-------------------
文件         | 函数覆盖率 | 代码行覆盖率 | 未覆盖的行号
-------------|---------|---------|-------------------
所有文件     |   66.67 |   77.78 |
 math.ts     |   50.00 |   66.67 |
 random.ts   |   50.00 |   66.67 |
-------------|---------|---------|-------------------

 3 通过
 0 失败
 3 次 expect() 调用
```

***

要设置最小覆盖率阈值，请将以下内容添加到你的 `bunfig.toml`。这要求你的代码库有 90% 的测试覆盖率。

```toml bunfig.toml icon="settings" theme={"theme":{"light":"github-light","dark":"dracula"}}
[test]
# 要求 90% 的代码行和函数覆盖率
coverageThreshold = 0.9
```

***

如果你的测试套件未达到此阈值，`bun test` 将以非零退出码退出，表示测试失败。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun test --coverage
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
<测试输出>
$ echo $?
1 # 这是前一个命令的退出码
```

***

可以分别为代码行覆盖率和函数覆盖率设置不同阈值。

```toml bunfig.toml icon="settings" theme={"theme":{"light":"github-light","dark":"dracula"}}
[test]
# 为代码行和函数设置不同的阈值
coverageThreshold = { lines = 0.5, functions = 0.7 }
```

***

完整的 Bun 代码覆盖率报告文档，请参见 [文档 > 测试运行器 > 覆盖率](/test/code-coverage)。
