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

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

Bun 的测试运行器内置了代码覆盖率报告。使用 `--coverage` 标志启用。

```sh terminal icon="terminal" theme={null}
bun test --coverage
```

```txt theme={null}
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` 中。阈值为 `0.9` 要求测试覆盖代码库的 90%。

```toml bunfig.toml icon="settings" theme={null}
[test]
# 要求 90% 的行级和函数级覆盖率
coverageThreshold = 0.9
```

***

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

```sh terminal icon="terminal" theme={null}
bun test --coverage
```

```txt theme={null}
<测试输出>
$ echo $?
1 # 这是上一条命令的退出码
```

***

你可以分别为行级和函数级覆盖率设置不同的阈值。

```toml bunfig.toml icon="settings" theme={null}
[test]
# 为行和函数设置不同的阈值
coverageThreshold = { lines = 0.5, functions = 0.7 }
```

***

参见 [代码覆盖率](/test/code-coverage)。
