From be3c65899932fd3df03b78c5644bbc7c2bbcb8f3 Mon Sep 17 00:00:00 2001 From: Clayton Watts Date: Tue, 23 Aug 2016 15:31:10 -0600 Subject: [PATCH] Allow relative path to custom reporter Closes #2434 Previously, mocha --reporter=./path/to/custom-reporter.js would fail. Also removes code from _mocha that has been unnecessary since 191b88c079d44f527dfeb022fbdb4079440c8428 --- bin/_mocha | 13 ------------- lib/mocha.js | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/bin/_mocha b/bin/_mocha index 11d3d0c662..de40acfabd 100755 --- a/bin/_mocha +++ b/bin/_mocha @@ -206,19 +206,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 (err) { - throw new Error('reporter "' + program.reporter + '" does not exist'); - } -} - // --no-colors if (!program.colors) mocha.useColors(false); diff --git a/lib/mocha.js b/lib/mocha.js index a649715191..506a66827a 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -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') {