Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
--coverage-report
related options.
- Defaults to summary, but can be set to any Istanbul report option, such as lcov, html, json, etc. - `--coverage-dir` is where coverage reporter are written. Defaults to ./coverage.
- Loading branch information
Showing
3 changed files
with
95 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"use strict"; | ||
|
||
/** | ||
* @module reporter-coverage | ||
*/ | ||
|
||
var util = require("util"); | ||
var istanbul; | ||
|
||
var Reporter = require("./reporter"); | ||
var CoverageSummaryReporter = require("./coverage-summary"); | ||
|
||
/** | ||
* @class CoverageReporter | ||
* @constructor | ||
* @extends Reporter | ||
*/ | ||
function CoverageReporter(options) { | ||
Reporter.call(this, options); | ||
|
||
try { | ||
istanbul = require("istanbul"); | ||
} catch (ex) { | ||
throw new Error("Unable to report coverage unless Istanbul is installed"); | ||
} | ||
|
||
this.collector = new istanbul.Collector(); | ||
|
||
this.istanbulReporter = istanbul.Report.create( | ||
this.options.get("coverage-report"), | ||
{ | ||
dir: this.options.get("coverage-dir") | ||
} | ||
); | ||
|
||
this.complete = false; | ||
} | ||
|
||
CoverageReporter.create = function (config) { | ||
var reporter = CoverageReporter; | ||
|
||
if (config.options.get("coverage-report") === "summary") { | ||
reporter = CoverageSummaryReporter; | ||
} | ||
|
||
return new reporter(config); | ||
}; | ||
|
||
util.inherits(CoverageReporter, Reporter); | ||
|
||
CoverageReporter.prototype.bindEvents = function () { | ||
this.batch.on("agentResult", this.handleAgentResult.bind(this)); | ||
this.batch.once("complete", this.handleComplete.bind(this)); | ||
this.batch.once("end", this.handleEnd.bind(this)); | ||
}; | ||
|
||
CoverageReporter.prototype.handleAgentResult = function (agent, details) { | ||
if (details.coverage) { | ||
this.collector.add(details.coverage); | ||
} | ||
}; | ||
|
||
CoverageReporter.prototype.handleComplete = function () { | ||
this.istanbulReporter.writeReport(this.collector, /* sync */ true); | ||
this.cli.error(util.format( | ||
"Wrote %s coverage report to %s", | ||
this.options.get("coverage-report"), | ||
this.options.get("coverage-dir") | ||
)); | ||
this.complete = true; | ||
}; | ||
|
||
CoverageReporter.prototype.handleEnd = function () { | ||
if (!this.complete) { this.handleComplete(); } | ||
this.emit("end"); | ||
}; | ||
|
||
module.exports = CoverageReporter; |