diff --git a/lib/v8-to-istanbul.js b/lib/v8-to-istanbul.js index 86b07d6c..317c2d0f 100644 --- a/lib/v8-to-istanbul.js +++ b/lib/v8-to-istanbul.js @@ -273,8 +273,10 @@ module.exports = class V8ToIstanbul { s: {} } source.lines.forEach((line, index) => { + const srcLine = source.lines[line.startLine - 1] + const ignore = srcLine === undefined ? true : srcLine.ignore statements.statementMap[`${index}`] = line.toIstanbul() - statements.s[`${index}`] = line.count + statements.s[`${index}`] = ignore ? 1 : line.count }) return statements } diff --git a/tap-snapshots/test/v8-to-istanbul.js.test.cjs b/tap-snapshots/test/v8-to-istanbul.js.test.cjs index 8d5d5c47..65102508 100644 --- a/tap-snapshots/test/v8-to-istanbul.js.test.cjs +++ b/tap-snapshots/test/v8-to-istanbul.js.test.cjs @@ -360,8 +360,8 @@ Object { "0": 1, "1": 1, "10": 1, - "11": 0, - "12": 0, + "11": 1, + "12": 1, "13": 1, "14": 1, "15": 1, @@ -1256,14 +1256,14 @@ Object { "30": 1, "31": 1, "32": 1, - "33": 0, - "34": 0, - "35": 0, + "33": 1, + "34": 1, + "35": 1, "36": 1, - "37": 0, + "37": 1, "38": 1, "39": 1, - "4": 0, + "4": 1, "40": 1, "41": 1, "42": 1, @@ -1272,10 +1272,10 @@ Object { "45": 1, "46": 1, "47": 1, - "5": 0, - "6": 0, - "7": 0, - "8": 0, + "5": 1, + "6": 1, + "7": 1, + "8": 1, "9": 1, }, "statementMap": Object { @@ -2330,16 +2330,16 @@ Object { }, }, "s": Object { - "0": 0, + "0": 1, "1": 1, "10": 1, "11": 1, "12": 1, "13": 1, - "14": 0, - "15": 0, - "16": 0, - "17": 0, + "14": 1, + "15": 1, + "16": 1, + "17": 1, "18": 1, "19": 1, "2": 1, @@ -2351,7 +2351,7 @@ Object { "25": 1, "26": 1, "27": 1, - "28": 0, + "28": 1, "29": 1, "3": 1, "30": 1, @@ -2936,7 +2936,7 @@ Object { "22": 1, "23": 1, "24": 1, - "25": 0, + "25": 1, "26": 1, "27": 1, "28": 1, @@ -2948,18 +2948,18 @@ Object { "33": 1, "34": 1, "35": 1, - "36": 0, - "37": 0, + "36": 1, + "37": 1, "38": 1, "39": 1, "4": 1, "40": 1, "41": 1, "42": 1, - "43": 0, - "44": 0, - "45": 0, - "46": 0, + "43": 1, + "44": 1, + "45": 1, + "46": 1, "47": 1, "48": 1, "49": 1, @@ -2969,25 +2969,25 @@ Object { "52": 1, "53": 1, "54": 1, - "55": 0, - "56": 0, - "57": 0, - "58": 0, - "59": 0, + "55": 1, + "56": 1, + "57": 1, + "58": 1, + "59": 1, "6": 1, - "60": 0, - "61": 0, - "62": 0, + "60": 1, + "61": 1, + "62": 1, "63": 1, "64": 1, "65": 1, "66": 1, - "67": 0, - "68": 0, - "69": 0, + "67": 1, + "68": 1, + "69": 1, "7": 1, - "70": 0, - "71": 0, + "70": 1, + "71": 1, "72": 1, "73": 1, "74": 1, diff --git a/test/fixtures/scripts/ignored.lines.js b/test/fixtures/scripts/ignored.lines.js new file mode 100644 index 00000000..f9570e9f --- /dev/null +++ b/test/fixtures/scripts/ignored.lines.js @@ -0,0 +1,4 @@ +/* c8 ignore next 3 */ +function sum(a, b) { + return a + b; +}; \ No newline at end of file diff --git a/test/v8-to-istanbul.js b/test/v8-to-istanbul.js index 7a2c6938..4cf670e6 100644 --- a/test/v8-to-istanbul.js +++ b/test/v8-to-istanbul.js @@ -139,6 +139,32 @@ ${'//'}${'#'} sourceMappingURL=data:application/json;base64,${base64Sourcemap} }]) Object.keys(v8ToIstanbul.toIstanbul()).should.eql(['/src/index.ts', '/src/utils.ts'].map(path.normalize)) }) + + it('ignore hint marks statements of uncovered file as covered', async () => { + const filename = require.resolve('./fixtures/scripts/ignored.lines.js') + const source = readFileSync(filename, 'utf-8') + const v8ToIstanbul = new V8ToIstanbul(pathToFileURL(filename).href) + await v8ToIstanbul.load() + + v8ToIstanbul.applyCoverage([ + { + functionName: '(empty-report)', + ranges: [ + { + startOffset: 0, + endOffset: source.length, + count: 0 + } + ], + isBlockCoverage: true + } + ]) + + const coverageMap = v8ToIstanbul.toIstanbul() + const { s } = coverageMap[filename] + + assert.deepStrictEqual(s, { 0: 1, 1: 1, 2: 1, 3: 1 }) + }) }) describe('source map format edge cases', () => {