From 18ea188d98b421e5f9fda215c02356f829d55a1d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 27 Aug 2016 12:12:36 +0200 Subject: [PATCH] cleanup: simplify istanbul-lib-coverage API usage - Deduplicate merging of all coverage files - Use the `map.getCoverageSummary()` convenience method - Use convenience getters of `CoverageSummary` objects --- index.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 4d10f8102..73aaae9da 100755 --- a/index.js +++ b/index.js @@ -391,17 +391,23 @@ function coverageFinder () { return coverage } -NYC.prototype.report = function () { - var tree +NYC.prototype._getCoverageMapFromAllCoverageFiles = function () { var map = libCoverage.createCoverageMap({}) - var context = libReport.createContext({ - dir: this._reportDir - }) this._loadReports().forEach(function (report) { map.merge(report) }) + return map +} + +NYC.prototype.report = function () { + var tree + var map = this._getCoverageMapFromAllCoverageFiles() + var context = libReport.createContext({ + dir: this._reportDir + }) + tree = libReport.summarizers.pkg(map) this.reporter.forEach(function (_reporter) { @@ -410,22 +416,12 @@ NYC.prototype.report = function () { } NYC.prototype.checkCoverage = function (thresholds) { - var map = libCoverage.createCoverageMap({}) - var summary = libCoverage.createCoverageSummary() - - this._loadReports().forEach(function (report) { - map.merge(report) - }) - - map.files().forEach(function (f) { - var fc = map.fileCoverageFor(f) - var s = fc.toSummary() - summary.merge(s) - }) + var map = this._getCoverageMapFromAllCoverageFiles() + var summary = map.getCoverageSummary() // ERROR: Coverage for lines (90.12%) does not meet global threshold (120%) Object.keys(thresholds).forEach(function (key) { - var coverage = summary.data[key].pct + var coverage = summary[key].pct if (coverage < thresholds[key]) { process.exitCode = 1 console.error('ERROR: Coverage for ' + key + ' (' + coverage + '%) does not meet global threshold (' + thresholds[key] + '%)')