Skip to content

Implicit branches are not considered in coverage #2239

@bencelang

Description

@bencelang

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.3

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: coverageIssues and PRs related to the coverage feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions