istanbuljs / v8-to-istanbul

convert from v8 coverage format to istanbul's format
ISC License
115 stars 40 forks source link

Ignore hint does not mark lines and statements of uncovered files #217

Closed AriPerkkio closed 1 year ago

AriPerkkio commented 1 year ago

When uncovered file contains ignore hints, the statements and lines are not marked as 100. Branches and functions are correctly marked as covered.

Reproduction

Minimal, without test runner

Save as repro.mjs and run as node repro.mjs. See how branches and functions are covered but statements are not.

import v8ToIstanbul from "v8-to-istanbul";
import { writeFileSync } from "node:fs";

const source = `
/* c8 ignore next 3 */
module.exports = function sum(a, b) {
  return a + b;
};
`.trim();

writeFileSync("test-file.js", source, "utf-8");

const converter = v8ToIstanbul("./test-file.js", 0, { source });
await converter.load();

converter.applyCoverage([
  {
    functionName: "(empty-report)",
    ranges: [
      {
        startOffset: 0,
        endOffset: source.length,
        count: 0,
      },
    ],
    isBlockCoverage: true,
  },
]);

const coverageMap = converter.toIstanbul();
const [fileCoverage] = Object.values(coverageMap);
const { b, f, s } = fileCoverage;

console.log({ b, f, s });

/*
$ node coverage-test.mjs
{
  b: { '0': [ 1 ] },
  f: { '0': 1 },
  s: { '0': 0, '1': 0, '2': 0, '3': 0 }
}
*/

With c8

c8-repro
├── index.js
├── package.json
└── temp.js
// index.js
console.log("Hello world");
// temp.js
/* c8 ignore next 3 */
module.exports = function sum(a, b) {
  return a + b;
};
{
  "name": "c8-repro",
  "scripts": {
    "c8": "c8"
  },
  "dependencies": {
    "c8": "^8.0.0"
  }
}
$ pnpm run c8 --all node index.js 

> c8 "--all" "node" "index.js"

Hello world
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |      20 |      100 |     100 |      20 |                   
 index.js |     100 |      100 |     100 |     100 |                   
 temp.js  |       0 |      100 |     100 |       0 | 1-4               
----------|---------|----------|---------|---------|-------------------