Skip to content

coverage-v8 issues with calculation of branch coverage #6300

@stereobooster

Description

@stereobooster

Describe the bug

I have following code

export function comp(a, b) {
  if (a === b) {
    return 0
  } else if (a > b) {
    return 1
  } else {
    return -1
  }
}

and following test

test("compares", () => {
  expect(comp(1, 1)).toBe(0);
  expect(comp(1, 0)).toBe(1);
});

it shows branch coverage 2/3 - which is correct. But If I add missing test

expect(comp(0, 1)).toBe(-1);

It shows branch coverage 4/4. Which seems to be incorrect

If I change code to

export function comp(a, b) {
  if (a === b) {
    return 0
  } else if (a > b) {
    return 1
  } 
  return -1
}

It shows branch coverage 5/5. Which seems to be incorrect.

PS 1 I checked it with istanbul, and istanbul numbers are more consistent. So I guess this is the issue with c8

PS 2 I checked using c8 (^10.1.2) directly and results are different, so there is a bug

Reproduction

https://github.com/stereobooster/vitest-coverage-v8

System Info

System:
    OS: macOS 14.1.1
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 196.43 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
    pnpm: 9.2.0 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 127.0.6533.99
    Safari: 17.1
  npmPackages:
    @vitest/coverage-v8: ^2.0.3 => 2.0.3
    @vitest/ui: ^2.0.3 => 2.0.3
    vite: ^5.3.4 => 5.3.4
    vitest: ^2.0.3 => 2.0.

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: coverageIssues and PRs related to the coverage featureupstream

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions