Skip to content

Commit

Permalink
fix: coverage for arrow functions returning literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Jul 27, 2024
1 parent c306580 commit 801f15c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/modules/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions test/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
3 changes: 3 additions & 0 deletions test/coverage/single-line-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 801f15c

Please sign in to comment.