Skip to content

Commit

Permalink
Added support to load reporters from a path (absolute or relative) (#39)
Browse files Browse the repository at this point in the history
* Added support to load reporters from a path (absolute or relative)

* Added unit test

* Upgraded NMP dependencies due to failing on bitHound - Dependencies checks

* Downgraded jenkins-mocha to v2.6.0 since latest version uses ES6

* Downgraded NPM dependencies because latest versions uses ES6 not ES5
  • Loading branch information
afshinhm authored and stanleyhlng committed Sep 19, 2017
1 parent 62f39a8 commit a083c96
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
16 changes: 13 additions & 3 deletions lib/MultiReporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,19 @@ function MultiReporters(runner, options) {
Reporter = require(name);
}
catch (err) {
err.message.indexOf('Cannot find module') !== -1 ?
console.warn('"' + name + '" reporter not found') : console.warn('"' + name + '" reporter blew up with error:\n' + err.stack);
Reporter = null;
if (err.message.indexOf('Cannot find module') !== -1) {
// Try to load reporters from a path (absolute or relative)
try {
Reporter = require(path.resolve(process.cwd(), name));
}
catch (_err) {
_err.message.indexOf('Cannot find module') !== -1 ? console.warn('"' + name + '" reporter not found')
: console.warn('"' + name + '" reporter blew up with error:\n' + _err.stack);
}
}
else {
console.warn('"' + name + '" reporter blew up with error:\n' + err.stack);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/custom-internal-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"reporterEnabled": "dot",
"reporterEnabled": "dot, tests/custom-internal-reporter",

"xunitReporterOptions": {
"output": "artifacts/test/custom-xunit.xml"
Expand Down
12 changes: 12 additions & 0 deletions tests/custom-internal-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var mocha = require('mocha');
var Base = mocha.reporters.Base;

/**
* This is a project-local custom reporter which is used as a stub in tests
* to verify if loading reporters from a path (absolute or relative) is successful
*/
function CustomReporterStub(runner) {
Base.call(this, runner);
}

module.exports = CustomReporterStub;
29 changes: 24 additions & 5 deletions tests/lib/MultiReporters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ describe('lib/MultiReporters', function () {
});

describe('#instance', function () {
var mocha;
var suite;
var runner;
var reporter;
var options;

describe('#internal', function () {
beforeEach(function () {
var mocha = new Mocha({
mocha = new Mocha({
reporter: MultiReporters
});
suite = new Suite('#internal-multi-reporter', 'root');
Expand Down Expand Up @@ -135,7 +136,7 @@ describe('lib/MultiReporters', function () {

it('return custom options', function () {
expect(reporter.getCustomOptions(options)).to.be.deep.equal({
reporterEnabled: 'dot',
reporterEnabled: 'dot, tests/custom-internal-reporter',
xunitReporterOptions: {
output: 'artifacts/test/custom-xunit.xml'
}
Expand All @@ -144,7 +145,7 @@ describe('lib/MultiReporters', function () {

it('return resultant options by merging both default and custom options', function () {
expect(reporter.getOptions(options)).to.be.deep.equal({
reporterEnabled: 'dot',
reporterEnabled: 'dot, tests/custom-internal-reporter',
reporterOptions: {
id: 'default'
},
Expand All @@ -161,6 +162,24 @@ describe('lib/MultiReporters', function () {
});
});
});

describe('#custom-internal-reporter', function () {
beforeEach(function() {
options = {
execute: true,
reporterOptions: {
configFile: 'tests/custom-internal-config.json'
}
};
reporter = new mocha._reporter(runner, options);
});

it('return default options for "custom-internal-reporter"', function () {
expect(reporter.getReporterOptions(reporter.getOptions(options), 'custom-internal-reporter')).to.be.deep.equal({
id: 'default',
});
});
});
});

describe('#external', function () {
Expand All @@ -173,7 +192,7 @@ describe('lib/MultiReporters', function () {

describe('json', function() {
beforeEach(function () {
var mocha = new Mocha({
mocha = new Mocha({
reporter: MultiReporters
});
suite = new Suite('#external-multi-reporter', 'root');
Expand All @@ -196,7 +215,7 @@ describe('lib/MultiReporters', function () {

describe('js', function() {
beforeEach(function () {
var mocha = new Mocha({
mocha = new Mocha({
reporter: MultiReporters
});
suite = new Suite('#external-multi-reporter', 'root');
Expand Down

0 comments on commit a083c96

Please sign in to comment.