测试 Coverage
Measure code coverage, 设置 thresholds, and 解释执行什么 coverage really tells 你.
你将学到
- 什么行, 分支, 函数, and 语句 coverage 平均值
- 如何 collect coverage 使用 Vitest and Jest (v8 / istanbul)
- 设置 coverage thresholds in CI
- 为什么 100% coverage 做不 guarantee bug-free code
概念讲解
What Coverage Measures
Code coverage instruments your code so the test runner can record which lines, branches, functions, and statements were executed during the test run. Line coverage is the percentage of executable lines that ran. Branch coverage tracks both sides of every if and ternary. Function coverage tracks which functions were called. Statement coverage is similar to line coverage but counts statements, which can differ when multiple statements share a line.
Collecting Coverage
In Vitest, run vitest 运行 --coverage; in Jest, jest --coverage. Both support the V8 instrumentation (fast, native) and the istanbul-style instrumentation (more accurate for edge cases). The output is a table per file plus HTML and LCOV reports you can open in a browser to see exactly which lines are red (uncovered) and green (covered).
Thresholds in CI
Set coverage thresholds so coverage cannot silently drop. In Vitest config, coverage.thresholds.行 = 80 fails the run if line coverage falls below 80%. Enforce thresholds in CI to prevent merges that reduce coverage. Start with a realistic target (70–80%) and raise it gradually; demanding 100% on a legacy codebase blocks productivity without adding safety.
Coverage Is a Signal, Not a Goal
High coverage means code was executed, not that it was meaningfully tested. A test that calls a function with no assertions still counts as covered. 100% coverage does not prove correctness, edge cases, or that the right thing happens for every input. Use coverage to find untested code, but judge test quality by whether tests assert meaningful behavior and catch real bugs.
In Practice
Treat the coverage report as a map of where to look next, not a scoreboard to maximize. When you see a red line, ask "什么 behavior lives 这里, and 什么测试会 prove 它?" — then write a test with real assertions, not a call that merely executes the line. Pay attention to branch coverage especially: a function with 100% line coverage can still have untested 如果 branches that hide bugs. Set a threshold that is achievable today (say 75%) and ratchet it up as the suite matures; a threshold that blocks every PR just gets disabled. In code review, glance at the coverage diff — a PR that drops coverage by 5% deserves a question about which behavior is now untested. Coverage is a useful servant but a terrible master; let it guide you, not drive you.
示例代码
// vitest.config.ts — coverage configuration
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
coverage: {
provider: "v8", // fast, built into Node
reporter: ["text", "html", "lcov"],
include: ["src/**/*.{ts,tsx}"],
exclude: ["src/**/*.d.ts"],
thresholds: {
lines: 80,
branches: 75,
functions: 80,
statements: 80,
},
},
},
});
// Run: npx vitest run --coverage
// Fails the build if any threshold is not met.
v8 provider 是 fastest. Thresholds 制作 coverage gate: 如果 PR drops below 80% 行, CI 运行失败 and 合并是 blocked.
动手试一试
- Code Diff
See 哪个行 coverage report marks as uncovered.
- JSON Formatter
读取 JSON coverage summary produced for CI dashboards.
常见错误
错误写法
Chasing 100% coverage 使用 assertion-free 测试
正确做法
Coverage 不使用 assertions 是 theater. 编写测试那个会 失败如果 code broke, 然后 let coverage follow naturally.
错误写法
Ignoring 分支 coverage and 仅 watching 行
正确做法
行 coverage 能是 100% 虽然 half 如果-分支是 untested. 总是 track 分支 coverage 也.
练习 测试 Coverage
6 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。