Skip to content

Commit

Permalink
fix: normalize coverage data, to correct for bad coverage output
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Apr 16, 2017
1 parent 9f2f18a commit 82a4715
Show file tree
Hide file tree
Showing 4 changed files with 465 additions and 3 deletions.
24 changes: 24 additions & 0 deletions packages/istanbul-lib-coverage/lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function assertValidSummary(obj) {
Object.keys(obj).join(','));
}
}

/**
* CoverageSummary provides a summary of code coverage . It exposes 4 properties,
* `lines`, `statements`, `branches`, and `functions`. Each of these properties
Expand Down Expand Up @@ -123,6 +124,28 @@ function assertValidObject(obj) {
Object.keys(obj).join(','));
}
}
// some coverage tools in the wild manage to output quirky data,
// we clean this up as best we can.
function normalizeCoverageObject(obj) {
// delete any keys from our counts, that don't have a corresponding
// map, see: https://github.com/istanbuljs/istanbuljs/issues/28
['statementMap', 'fnMap', 'branchMap'].forEach(function (mapType) {
var counts = obj[mapType.charAt(0)];
var map = obj[mapType];
if (typeof counts === 'object' && typeof map === 'object') {
var ckeys = Object.keys(counts);
var mkeys = Object.keys(obj[mapType]);
if (ckeys.length !== mkeys.length) {
ckeys.forEach(function (k) {
if (!map[k]) {
delete counts[k];
}
});
}
}
});
}

/**
* provides a read-only view of coverage for a single file.
* The deep structure of this object is documented elsewhere. It has the following
Expand Down Expand Up @@ -155,6 +178,7 @@ function FileCoverage(pathOrObj) {
throw new Error('Invalid argument to coverage constructor');
}
assertValidObject(this.data);
normalizeCoverageObject(this.data);
}
/**
* returns computed line coverage from statement coverage.
Expand Down
1 change: 0 additions & 1 deletion packages/istanbul-reports/lib/html/annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,3 @@ function annotateSourceCode(fileCoverage, sourceStore) {
module.exports = {
annotateSourceCode: annotateSourceCode
};

Loading

0 comments on commit 82a4715

Please sign in to comment.