Skip to content

Commit

Permalink
index.js: Fallback to require(...) for unrecognized reporters
Browse files Browse the repository at this point in the history
This allows us to use reporters that aren't built in.  For example,
with this commit you can:

  $ npm install
  $ npm install --no-save mocha mochawesome
  $ cp index.js node_modules/tap-mocha-reporter/
  $ cp lib/*.js node_modules/tap-mocha-reporter/lib/
  $ npm test -- --reporter=mochawesome

That's installing [email protected] and [email protected].
  • Loading branch information
wking committed Jun 8, 2018
1 parent 7d50e22 commit 8b73357
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ function Formatter (type, options) {
if (!(this instanceof Formatter)) {
return new Formatter(type, options)
}
if (!reporters[type]) {
var _reporter = reporters[type];
if (!_reporter) {
try {
_reporter = require(type);
} catch (err) {
console.warn(err);
}
}
if (!_reporter) {
console.error('Unknown format type: %s\n\n%s', type, avail())
type = 'silent'
}
Expand All @@ -50,7 +58,7 @@ function Formatter (type, options) {
}

var runner = this.runner = new Runner(options)
var reporter = this.reporter = new reporters[type](this.runner, {})
var reporter = this.reporter = new _reporter(this.runner, {})
Writable.call(this, options)

runner.on('end', function () {
Expand Down

0 comments on commit 8b73357

Please sign in to comment.