Skip to content

Commit

Permalink
Allow relative path to custom reporter
Browse files Browse the repository at this point in the history
Closes mochajs#2434

Previously, mocha --reporter=./path/to/custom-reporter.js would fail.

Also removes code from _mocha that has been unnecessary since 191b88c
  • Loading branch information
cletusw committed Sep 6, 2016
1 parent 4a87a94 commit 3f25af7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
13 changes: 0 additions & 13 deletions bin/_mocha
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,6 @@ if (program.reporterOptions !== undefined) {

mocha.reporter(program.reporter, reporterOptions);

// load reporter

var Reporter = null;
try {
Reporter = require('../lib/reporters/' + program.reporter);
} catch (err) {
try {
Reporter = require(program.reporter);
} catch (err2) {
throw new Error('reporter "' + program.reporter + '" does not exist');
}
}

// --no-colors

if (!program.colors) {
Expand Down
18 changes: 14 additions & 4 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,24 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) {
if (reporters[reporter]) {
_reporter = reporters[reporter];
}
// Try to load reporters from process.cwd() and node_modules
// Try to load reporters from process.cwd()
if (!_reporter) {
try {
_reporter = require(path.resolve(reporter));
} catch (err) {
if (err.message.indexOf('Cannot find module') === -1) {
console.warn('"' + reporter + '" reporter blew up with error:\n' + err.stack);
}
}
}
// Try to load reporters from node_modules
if (!_reporter) {
try {
_reporter = require(reporter);
} catch (err) {
err.message.indexOf('Cannot find module') !== -1
? console.warn('"' + reporter + '" reporter not found')
: console.warn('"' + reporter + '" reporter blew up with error:\n' + err.stack);
if (err.message.indexOf('Cannot find module') === -1) {
console.warn('"' + reporter + '" reporter blew up with error:\n' + err.stack);
}
}
}
if (!_reporter && reporter === 'teamcity') {
Expand Down

0 comments on commit 3f25af7

Please sign in to comment.