-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Describe the bug
Code with implicit branches is not detected in coverage. Consider the following code:
function fn(param) {
if (param) {
// If param is falsy, this if never runs
}
}If test code is only executing fn with a truthy parameter the implicit else branch is never tested, thus should not count towards covered branches. Interestingly enough, coverage still reports 100% for branch coverage.
This impacts both coverage providers but in case of c8 it is a known issue. With istanbul it is expected to report the correct 50% branch coverage metric, yet running under vitest the problem persists. Read reproduction for details.
Reproduction
Consider the following code (origin):
// cov.js
export const fn = y => {
if (typeof y !== 'object') {
y = 'yup'
}
console.log(y)
}// cov.spec.js
import { fn } from "cov"
test("implicit branch coverage bug", () => {
const x = 'asdf'
fn(x)
});The coverage report using @vitest/coverage-c8 reports this as 100% branch coverage, due to a known issue with v8 (bcoe/c8#227 and this v8 ticket). However, running a sligthly different version of this code from the gist I linked above in commonjs syntax through nyc node cov.cjs prints 50%, correctly. (ESM code causes nyc to have a blank output but that is also already a known issue).
Interestingly, switching the coverage provider to @vitest/coverage-istanbul is still impacted, showing full coverage mistakenly.
% Coverage report from istanbul
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
cov.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
System Info
system: Windows 11 Build 10.0.22621
node: v19.0.0
pnpm: v6.32.9
nyc: v15.1.0
vitest: v0.24.3Used Package Manager
pnpm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.