Skip to content

Commit

Permalink
fix: Drop unneeded coverage data from nyc --all (#456)
Browse files Browse the repository at this point in the history
Frequently users run `nyc --all` in a way that causes source files to be
transpiled for actual testing but not transpiled for `--all`.  This
produces incompatible coverage data and inconsistantly wrong reporting.

The work around here is to drop coverage produced by `--all` for any
file where we have coverage produced by actual test runs.  This ensures
that we prefer code that was transpiled in the way which tests actually
ran.

Fixes #123, #224, #260, #322, #413
  • Loading branch information
coreyfarrell committed Aug 2, 2019
1 parent a3ebc6f commit f6bb0b4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/istanbul-lib-coverage/lib/file-coverage.js
Expand Up @@ -157,6 +157,15 @@ class FileCoverage {
* Note that the other object should have the same structure as this one (same file).
*/
merge(other) {
if (other.all === true) {
return;
}

if (this.all === true) {
this.data = other.data;
return;
}

Object.entries(other.s).forEach(([k, v]) => {
this.data.s[k] += v;
});
Expand Down Expand Up @@ -246,7 +255,8 @@ dataProperties(FileCoverage, [
'branchMap',
's',
'f',
'b'
'b',
'all'
]);

module.exports = {
Expand Down
69 changes: 69 additions & 0 deletions packages/istanbul-lib-coverage/test/file.test.js
Expand Up @@ -260,6 +260,75 @@ describe('base coverage', () => {
assert.equal(c1.b[1][1], 2);
});

it('drops all data during merges', () => {
const loc = function(sl, sc, el, ec) {
return {
start: { line: sl, column: sc },
end: { line: el, column: ec }
};
};
const template = new FileCoverage({
path: '/path/to/file',
statementMap: {
1: loc(1, 1, 1, 100),
2: loc(2, 1, 2, 50),
3: loc(2, 51, 2, 100),
4: loc(2, 101, 3, 100)
},
fnMap: {
1: {
name: 'foobar',
line: 1,
loc: loc(1, 1, 1, 50)
}
},
branchMap: {
1: {
type: 'if',
line: 2,
locations: [loc(2, 1, 2, 20), loc(2, 50, 2, 100)]
}
},
s: {
1: 0,
2: 0,
3: 0,
4: 0
},
f: {
1: 0
},
b: {
1: [0, 0]
}
});
const clone = function(obj) {
return JSON.parse(JSON.stringify(obj));
};
const createCoverage = all => {
const data = clone(template);
if (all) {
data.all = true;
} else {
data.s[1] = 1;
data.f[1] = 1;
data.b[1][0] = 1;
}

return new FileCoverage(data);
};

const expected = createCoverage().data;
// Get non-all data regardless of merge order
let cov = createCoverage(true);
cov.merge(createCoverage());
assert.deepEqual(cov.data, expected);

cov = createCoverage();
cov.merge(createCoverage(true));
assert.deepEqual(cov.data, expected);
});

it('resets hits when requested', () => {
const loc = function(sl, sc, el, ec) {
return {
Expand Down

0 comments on commit f6bb0b4

Please sign in to comment.