Skip to content

Commit

Permalink
Merge pull request #13 from stanleyhlng/external-reporters
Browse files Browse the repository at this point in the history
[Fix #12] Support custom external reporters
  • Loading branch information
stanleyhlng committed Oct 30, 2016
2 parents 867306e + 8ee1d05 commit 359cbf6
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 69 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ artifacts
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

.idea
22 changes: 19 additions & 3 deletions lib/MultiReporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,36 @@ function MultiReporters(runner, options) {

var deferred = Q.defer();
var reporterOptions = this.getReporterOptions(options, name);

//
// Mocha Reporters
// https://github.com/mochajs/mocha/blob/master/lib/reporters/index.js
//
var Reporter = _.get(mocha, ['reporters', name], null);

//
// External Reporters.
// Try to load reporters from process.cwd() and node_modules.
//
if (Reporter === null) {
try {
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 (Reporter !== null) {
new Reporter(
runner,
{
runner, {
reporterOptions: reporterOptions
}
);

runner.on('end', function() {
runner.on('end', function () {
deferred.resolve(name);
});
}
Expand Down
8 changes: 8 additions & 0 deletions tests/custom-external-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"reporterEnabled": "mocha-junit-reporter",

"mochaJunitReporterReporterOptions": {
"id": "mocha-junit-reporter",
"mochaFile": "junit.xml"
}
}
File renamed without changes.
160 changes: 94 additions & 66 deletions tests/lib/MultiReporters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('lib/MultiReporters', function () {
});

describe('#static', function () {
describe ('#CONFIG_FILE', function () {
describe('#CONFIG_FILE', function () {
it('equals to "../config.json"', function () {
expect(MultiReporters.CONFIG_FILE).to.be.equals('../config.json');
});
Expand All @@ -27,81 +27,109 @@ describe('lib/MultiReporters', function () {
var reporter;
var options;

beforeEach(function () {
var mocha = new Mocha({
reporter: MultiReporters
describe('#internal', function () {
beforeEach(function () {
var mocha = new Mocha({
reporter: MultiReporters
});
suite = new Suite('#internal-multi-reporter', 'root');
runner = new Runner(suite);
options = {
execute: false,
reporterOptions: {
configFile: 'tests/custom-internal-config.json'
}
};
reporter = new mocha._reporter(runner, options);
});
suite = new Suite('#multi-reporter', 'root');
runner = new Runner(suite);
options = {
execute: false,
reporterOptions: {
configFile: 'tests/custom-config.json'
}
};
reporter = new mocha._reporter(runner, options);
});

describe('#options (3rd-party: multi-reporters)', function () {
it('return default options', function () {
expect(reporter.getDefaultOptions()).to.be.deep.equal({
reporterEnabled: 'spec, xunit',
reporterOptions: {
id: 'default'
},
dotReporterOptions: {
describe('#options (reporters - single)', function () {
it('return reporter options: "dot"', function () {
expect(reporter.getReporterOptions(reporter.getOptions(options), 'dot')).to.be.deep.equal({
id: 'dot'
},
xunitReporterOptions: {
id: 'xunit',
output: 'xunit.xml'
},
tapReporterOptions: {
id: 'tap'
}
});
});
});

it('return custom options', function () {
expect(reporter.getCustomOptions(options)).to.be.deep.equal({
reporterEnabled: 'dot',
xunitReporterOptions: {
it('return reporter options: "xunit"', function () {
expect(reporter.getReporterOptions(reporter.getOptions(options), 'xunit')).to.be.deep.equal({
id: 'xunit',
output: 'artifacts/test/custom-xunit.xml'
}
});
});
});

it('return resultant options by merging both default and custom options', function () {
expect(reporter.getOptions(options)).to.be.deep.equal({
reporterEnabled: 'dot',
reporterOptions: {
id: 'default'
},
dotReporterOptions: {
id: 'dot'
},
xunitReporterOptions: {
id: 'xunit',
output: 'artifacts/test/custom-xunit.xml'
},
tapReporterOptions: {
id: 'tap'
}
describe('#options (reporters - multiple)', function () {
it('return default options', function () {
expect(reporter.getDefaultOptions()).to.be.deep.equal({
reporterEnabled: 'spec, xunit',
reporterOptions: {
id: 'default'
},
dotReporterOptions: {
id: 'dot'
},
xunitReporterOptions: {
id: 'xunit',
output: 'xunit.xml'
},
tapReporterOptions: {
id: 'tap'
}
});
});

it('return custom options', function () {
expect(reporter.getCustomOptions(options)).to.be.deep.equal({
reporterEnabled: 'dot',
xunitReporterOptions: {
output: 'artifacts/test/custom-xunit.xml'
}
});
});

it('return resultant options by merging both default and custom options', function () {
expect(reporter.getOptions(options)).to.be.deep.equal({
reporterEnabled: 'dot',
reporterOptions: {
id: 'default'
},
dotReporterOptions: {
id: 'dot'
},
xunitReporterOptions: {
id: 'xunit',
output: 'artifacts/test/custom-xunit.xml'
},
tapReporterOptions: {
id: 'tap'
}
});
});
});
});

describe('#options (built-in: mocha-* reporters)', function () {
it('return reporter options: "dot"', function () {
expect(reporter.getReporterOptions(reporter.getOptions(options), 'dot')).to.be.deep.equal({
id: 'dot'
describe('#external', function () {
beforeEach(function () {
var mocha = new Mocha({
reporter: MultiReporters
});
suite = new Suite('#external-multi-reporter', 'root');
runner = new Runner(suite);
options = {
execute: false,
reporterOptions: {
configFile: 'tests/custom-external-config.json'
}
};
reporter = new mocha._reporter(runner, options);
});

it('return reporter options: "xunit"', function () {
expect(reporter.getReporterOptions(reporter.getOptions(options), 'xunit')).to.be.deep.equal({
id: 'xunit',
output: 'artifacts/test/custom-xunit.xml'
describe('#options (external reporters - single)', function () {
it('return reporter options: "dot"', function () {
expect(reporter.getReporterOptions(reporter.getOptions(options), 'mocha-junit-reporter')).to.be.deep.equal({
id: 'mocha-junit-reporter',
mochaFile: 'junit.xml'
});
});
});
});
Expand Down Expand Up @@ -144,7 +172,7 @@ describe('lib/MultiReporters', function () {
});

runner.run(function (failureCount) {
expect(failureCount).to.be.equals(1);
expect(failureCount).to.equals(1);

// stats
expect(runner.stats).to.be.include({
Expand All @@ -156,14 +184,14 @@ describe('lib/MultiReporters', function () {
});

// suites
expect(runner.suite.title).to.be.equal('#multi-reporter');
expect(runner.suite.title).to.equal('#multi-reporter');
expect(runner.suite.tests).to.be.instanceof(Array);
expect(runner.suite.tests).to.have.length(2);

// test
var test = runner.suite.tests[1];
expect(test.title).to.be.equal('#test-2');
expect(test.state).to.be.equal('failed');
expect(test.title).to.equal('#test-2');
expect(test.state).to.equal('failed');

done();
});
Expand All @@ -184,7 +212,7 @@ describe('lib/MultiReporters', function () {
});

runner.run(function (failureCount) {
expect(failureCount).to.be.equals(0);
expect(failureCount).to.equals(0);

// stats
expect(runner.stats).to.be.include({
Expand All @@ -203,7 +231,7 @@ describe('lib/MultiReporters', function () {
// test
var test = runner.suite.tests[0];
expect(test.title).to.be.equal('#test-1');
expect(test.pending).to.be.true;
expect(test.pending).to.equal(true);

done();
});
Expand Down

0 comments on commit 359cbf6

Please sign in to comment.