npx skills add https://github.com/secondsky/claude-skills --skill 'Bun Test Coverage'Bun 内置代码覆盖率报告功能,无需额外依赖。
# 启用覆盖率
bun test --coverage
# 设置阈值(低于阈值则失败)
bun test --coverage --coverage-threshold 80
[test]
coverage = true
coverageThreshold = 0.8 # 最低 80%
coverageDir = "./coverage"
# 忽略模式
coverageSkipTestFiles = true
------------------|---------|---------|-------------------
File | % Funcs | % Lines | Uncovered Line #s
------------------|---------|---------|-------------------
All files | 85.71 | 89.23 |
src/index.ts | 100.00 | 100.00 |
src/utils.ts | 75.00 | 82.35 | 23-25, 41-43
src/api.ts | 80.00 | 85.00 | 67, 89-92
------------------|---------|---------|-------------------
# 默认控制台输出
bun test --coverage
# 生成 lcov 报告
bun test --coverage --coverage-reporter=lcov
# 多个报告器
bun test --coverage --coverage-reporter=text --coverage-reporter=lcov
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 报告器 | 输出 |
|---|---|
text | 控制台表格(默认) |
lcov | coverage/lcov.info 用于 CI 工具 |
json | coverage/coverage.json |
设置最低覆盖率要求:
# 如果覆盖率 < 80% 则失败
bun test --coverage --coverage-threshold 80
# 在 bunfig.toml 中按指标设置阈值
[test]
coverage = true
coverageThreshold = {
lines = 80,
functions = 75,
branches = 70
}
[test]
coverage = true
# 从覆盖率中跳过测试文件
coverageSkipTestFiles = true
# 排除模式
coverageIgnore = [
"**/*.test.ts",
"**/fixtures/**",
"**/mocks/**"
]
- name: 运行带覆盖率的测试
run: bun test --coverage --coverage-reporter=lcov
- name: 上传覆盖率到 Codecov
uses: codecov/codecov-action@v5
with:
files: ./coverage/lcov.info
# 自定义输出目录
bun test --coverage --coverage-dir=./reports/coverage
import { test, expect } from "bun:test";
// 以编程方式获取覆盖率数据
const coverage = Bun.coverage;
// 测试完成后访问
process.on("exit", () => {
console.log(coverage.getCoverageData());
});
| 错误 | 原因 | 修复方法 |
|---|---|---|
Coverage threshold not met | 覆盖率低于阈值 | 提高测试覆盖率 |
No coverage data | 文件未执行 | 检查测试是否包含该文件 |
lcov not found | 缺少报告器 | 添加 --coverage-reporter=lcov |
在以下情况加载 references/reporters.md:
每周安装次数
–
代码仓库
GitHub 星标数
93
首次出现时间
–
安全审计
Bun has built-in code coverage reporting without additional dependencies.
# Enable coverage
bun test --coverage
# With threshold (fail if below)
bun test --coverage --coverage-threshold 80
[test]
coverage = true
coverageThreshold = 0.8 # 80% minimum
coverageDir = "./coverage"
# Patterns to ignore
coverageSkipTestFiles = true
------------------|---------|---------|-------------------
File | % Funcs | % Lines | Uncovered Line #s
------------------|---------|---------|-------------------
All files | 85.71 | 89.23 |
src/index.ts | 100.00 | 100.00 |
src/utils.ts | 75.00 | 82.35 | 23-25, 41-43
src/api.ts | 80.00 | 85.00 | 67, 89-92
------------------|---------|---------|-------------------
# Default console output
bun test --coverage
# Generate lcov report
bun test --coverage --coverage-reporter=lcov
# Multiple reporters
bun test --coverage --coverage-reporter=text --coverage-reporter=lcov
| Reporter | Output |
|---|---|
text | Console table (default) |
lcov | coverage/lcov.info for CI tools |
json | coverage/coverage.json |
Set minimum coverage requirements:
# Fail if coverage < 80%
bun test --coverage --coverage-threshold 80
# Per-metric thresholds in bunfig.toml
[test]
coverage = true
coverageThreshold = {
lines = 80,
functions = 75,
branches = 70
}
[test]
coverage = true
# Skip test files from coverage
coverageSkipTestFiles = true
# Patterns to exclude
coverageIgnore = [
"**/*.test.ts",
"**/fixtures/**",
"**/mocks/**"
]
- name: Run tests with coverage
run: bun test --coverage --coverage-reporter=lcov
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
files: ./coverage/lcov.info
# Custom output directory
bun test --coverage --coverage-dir=./reports/coverage
import { test, expect } from "bun:test";
// Get coverage data programmatically
const coverage = Bun.coverage;
// Access after tests complete
process.on("exit", () => {
console.log(coverage.getCoverageData());
});
| Error | Cause | Fix |
|---|---|---|
Coverage threshold not met | Coverage below threshold | Increase test coverage |
No coverage data | Files not executed | Check test includes file |
lcov not found | Missing reporter | Add --coverage-reporter=lcov |
Load references/reporters.md when:
Weekly Installs
–
Repository
GitHub Stars
93
First Seen
–
Security Audits
GitHub Actions 官方文档查询助手 - 精准解答 CI/CD 工作流问题
30,000 周安装