Skip to content

Coverage for nullish coalescing operator #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/modules/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ global[internals._state] = global[internals._state] || {
_statement: function (name, id, line, source) {

const statement = global[internals._state].files[name].statements[line][id];

if (!statement.bool) {
statement.hit[!source] = true;
}

statement.hit[!!source] = true;
if (statement.bool === 'nullish') {
statement.hit[source !== null && source !== undefined] = true;
}
else {
statement.hit[!!source] = true;
}

return source;
},

Expand Down Expand Up @@ -151,12 +158,22 @@ internals.instrument = function (filename) {
const addStatement = function (line, node, bool) {

const id = ++ids;
statements.push({
const statement = {
id,
loc: node.loc,
line,
bool: bool && node.type !== 'ConditionalExpression' && node.type !== 'LogicalExpression'
});
};

// Node v14+ only
/*$lab:coverage:off$*/
if (statement.bool && node.parent.operator === '??') {
statement.bool = 'nullish';
}
/*$lab:coverage:on$*/

statements.push(statement);

return id;
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/lab-external-module-test": "1.x.x",
"@hapi/somever": "^3.0.1",
"@types/node": "^17.0.23",
"cpr": "3.x.x",
"lab-event-reporter": "1.x.x",
Expand Down
28 changes: 28 additions & 0 deletions test/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Path = require('path');
const Code = require('@hapi/code');
const _Lab = require('../test_runner');
const Lab = require('../');
const Somever = require('@hapi/somever');
const SupportsColor = require('supports-color');


Expand Down Expand Up @@ -36,6 +37,7 @@ const expect = Code.expect;

describe('Coverage', () => {

const supportsNullishCoalescing = Somever.match(process.version, '>=14');
Lab.coverage.instrument({ coveragePath: Path.join(__dirname, 'coverage'), coverageExclude: 'exclude' });

it('computes sloc without comments', async () => {
Expand Down Expand Up @@ -464,6 +466,20 @@ describe('Coverage', () => {
expect(missedLines).to.be.empty();
});

it('should measure coverage on nullish coalescing operator', { skip: !supportsNullishCoalescing }, async () => {

const Test = require('./coverage/conditional-coalesce');

expect(Test.method(false)).to.equal(false);
expect(Test.method()).to.equal('nullish');
expect(Test.method(null)).to.equal('nullish');

const cov = await Lab.coverage.analyze({ coveragePath: Path.join(__dirname, 'coverage/conditional-coalesce') });
const source = cov.files[0].source;
const missedLines = Object.keys(source).filter((lineNumber) => source[lineNumber].miss);
expect(missedLines).to.be.empty();
});

it('should measure missing coverage on conditional value', async () => {

const Test = require('./coverage/conditional-value2');
Expand All @@ -478,6 +494,18 @@ describe('Coverage', () => {
expect(missedLines).to.equal(['7']);
});

it('should measure missing coverage on nullish coalescing operator', { skip: !supportsNullishCoalescing }, async () => {

const Test = require('./coverage/conditional-coalesce2');

expect(Test.method(1, null)).to.equal('1secondMissing');

const cov = await Lab.coverage.analyze({ coveragePath: Path.join(__dirname, 'coverage/conditional-coalesce2') });
const source = cov.files[0].source;
const missedLines = Object.keys(source).filter((lineNumber) => source[lineNumber].miss);
expect(missedLines).to.equal(['13', '14']);
});

describe('analyze()', () => {

it('sorts file paths in report', async () => {
Expand Down
14 changes: 14 additions & 0 deletions test/coverage/conditional-coalesce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

// Load modules


// Declare internals

const internals = {};


exports.method = function (value) {

return value ?? 'nullish';
};
17 changes: 17 additions & 0 deletions test/coverage/conditional-coalesce2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

// Load modules


// Declare internals

const internals = {};


exports.method = function (first, second) {

const a = first ?? 'firstMissing';
const b = second ?? 'secondMissing';

return a + b;
};