From 801f15c2e8f7a93e469ff0df6b5b08bf74476630 Mon Sep 17 00:00:00 2001 From: Nicolas Morel Date: Sat, 27 Jul 2024 12:07:20 +0200 Subject: [PATCH] fix: coverage for arrow functions returning literals --- lib/modules/coverage.js | 2 +- test/coverage.js | 5 +++-- test/coverage/single-line-functions.js | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/modules/coverage.js b/lib/modules/coverage.js index 6fe2e1ef..60c323bb 100755 --- a/lib/modules/coverage.js +++ b/lib/modules/coverage.js @@ -307,7 +307,7 @@ internals.instrument = function (filename, ctx) { node.set(`(global.__$$labCov._statement(\'${filename}\',${left},${line},${node.left.source()})${node.operator}global.__$$labCov._statement(\'${filename}\',${right},${line},${node.right.source()}))`); } else if (node.parent?.type === 'ArrowFunctionExpression' && - node.type.includes('Expression')) { + (node.type.includes('Expression') || node.type === 'Literal')) { const id = addStatement(line, node, false); diff --git a/test/coverage.js b/test/coverage.js index f2635ff5..2cf34aab 100755 --- a/test/coverage.js +++ b/test/coverage.js @@ -484,12 +484,13 @@ describe('Coverage', () => { results.push(Test.method11(5, 10)); results.push(Test.method11(0, 10)); results.push(Test.method11Partial(5, 10)); + results.push(Test.method12()); const cov = await Lab.coverage.analyze({ coveragePath: Path.join(__dirname, 'coverage/single-line-functions') }); const source = cov.files[0].source; const missedLines = Object.keys(source).filter((lineNumber) => source[lineNumber].miss); - expect(results).to.equal([7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 10, 5]); - expect(missedLines).to.equal(['12', '15', '21', '27', '30', '33', '39', '46', '50', '53', '56']); + expect(results).to.equal([7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 10, 5, 42]); + expect(missedLines).to.equal(['12', '15', '21', '27', '30', '33', '39', '46', '50', '53', '56', '59']); }); it('should measure missing coverage on trailing function declarations correctly', async () => { diff --git a/test/coverage/single-line-functions.js b/test/coverage/single-line-functions.js index 103cd905..f00c1683 100644 --- a/test/coverage/single-line-functions.js +++ b/test/coverage/single-line-functions.js @@ -54,3 +54,6 @@ exports.method10NotCalled = (a, b) => exports.method9NotCalled(a, b); exports.method11 = (a, b) => a || b; exports.method11Partial = (a, b) => a || b; + +exports.method12 = () => 42; +exports.method12NotCalled = () => 42;