Sub topic 15
Measuring Code Coverage
This chapter assumes you have a working Vitest project with a vitest.config.js file, as set up in the configuration chapter, and that you can run tests from the terminal. Code coverage tells you which parts of your source code ran while your tests ran. It shows you where you have no tests. It does not tell you whether the tests you have are good.
Installing a Coverage Provider
A coverage provider is the package that records which code ran. Vitest supports two of them. v8 uses the coverage data built into the V8 JavaScript engine and is the default. istanbul adds tracking code to your source before it runs. Install the V8 provider as a dev dependency:
npm install -D @vitest/coverage-v8Then generate a report with a single run:
npx vitest run --coverageThe Four Metrics
Each report shows four numbers for every file.
| Metric | What it counts | Column | Column |
|---|---|---|---|
| Statements | Individual statements that executed | ||
| Branches | Each path through `if`, `? :`, `&&`, `\ | \ | `, and `switch` |
| Functions | Functions that were called at least once | ||
| Lines | Source lines that contained executed code |
Branch coverage usually finds gaps first. A file can reach 100% line coverage while a branch on one of those lines never runs.
Reading a Report
Here is a function with two branches and a test that covers only one of them:
// src/discount.js
export function applyDiscount(price, isMember) {
if (isMember) {
return price * 0.9
}
return price
}
// src/discount.test.js
import { it, expect } from 'vitest'
import { applyDiscount } from './discount.js'
it('gives members 10% off', () => {
expect(applyDiscount(100, true)).toBe(90)
})The text reporter prints a table like this one (the exact numbers depend on your provider version):
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
discount.js | 83.33 | 50 | 100 | 83.33 | 5
-------------|---------|----------|---------|---------|-------------------The "Uncovered Line #s" column points to line 5, the non-member return. Open coverage/index.html in a browser to see the same file with the lines that never ran highlighted.
Configuring Coverage
Add the options under test.coverage in your config:
// vitest.config.js
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
coverage: {
provider: 'v8',
reporter: ['text', 'html', 'lcov'],
include: ['src/**/*.js'],
exclude: ['src/**/*.test.js'],
thresholds: {
lines: 80,
branches: 80,
functions: 80,
statements: 80,
},
},
},
})includelists the files that belong in the report. Files that match the pattern but that no test imports appear at 0%, so untested files stay visible.lcovwrites a format that CI services and editor plugins can read.thresholdsmakesvitest run --coverageexit with an error when a metric falls below its value, which fails a CI build.
Common Mistakes
- Running coverage without the provider installed. Vitest stops with an error and asks you to install
@vitest/coverage-v8. - Committing the
coverage/folder. Add it to.gitignore. It is generated output. - Setting thresholds at 100%. This pushes people toward shallow tests of trivial code. Pick a level the team can maintain, then raise it over time.
Exercise
The report for discount.js shows 50% branch coverage. What single test would you add to bring branches to 100%, and why does line coverage also go up?
Show answer
Add a test such as expect(applyDiscount(100, false)).toBe(100). It runs the path where isMember is false, so the second branch of the if executes. That path includes the return on line 5, so line 5 becomes covered as well.
Exercise
Your CI build passes, but a file in src/ that no test imports is missing from the coverage report. What configuration change makes it appear?
Show answer
Set coverage.include to a pattern that matches it, such as ['src/*/.js']. Matching files that no test loaded then appear at 0% and count against the thresholds.