Skip to content

Commit

Permalink
Support done methods (#36)
Browse files Browse the repository at this point in the history
* support done methods
  • Loading branch information
chris-goodchild authored and stanleyhlng committed Aug 28, 2017
1 parent f5ea029 commit 16cb351
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 7 deletions.
46 changes: 39 additions & 7 deletions lib/MultiReporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

var _ = {
get: require('lodash/get'),
camelCase: require('lodash/camelCase')
camelCase: require('lodash/camelCase'),
size: require('lodash/size'),
after: require('lodash/after')
};
var debug = require('debug')('mocha:reporters:MultiReporters');
var fs = require('fs');
Expand All @@ -15,12 +17,11 @@ var path = require('path');
function MultiReporters(runner, options) {
Base.call(this, runner);

var promises = [];

if (_.get(options, 'execute', true)) {

options = this.getOptions(options);

_.get(options, 'reporterEnabled', 'tap').split(',').map(
this._reporters = _.get(options, 'reporterEnabled', 'tap').split(',').map(
function processReporterEnabled(name, index) {
debug(name, index);

Expand Down Expand Up @@ -50,7 +51,7 @@ function MultiReporters(runner, options) {
}

if (Reporter !== null) {
new Reporter(
return new Reporter(
runner, {
reporterOptions: reporterOptions
}
Expand All @@ -63,10 +64,38 @@ function MultiReporters(runner, options) {
);
}
}
util.inherits(MultiReporters, Base)

util.inherits(MultiReporters, Base);

MultiReporters.CONFIG_FILE = '../config.json';

MultiReporters.prototype.done = function (failures, fn) {
var numberOfReporters = _.size(this._reporters);

if (numberOfReporters === 0) {
console.error('Unable to invoke fn(failures) - no reporters were registered');
return;
}

var reportersWithDoneHandler = this._reporters.filter(function (reporter) {
return typeof reporter.done === 'function';
});

var numberOfReportersWithDoneHandler = _.size(reportersWithDoneHandler);

if (numberOfReportersWithDoneHandler === 0) {
return fn(failures);
}

var done = _.after(numberOfReportersWithDoneHandler, function() {
fn(failures);
});

reportersWithDoneHandler.forEach(function(reporter) {
reporter.done(failures, done);
});
};

MultiReporters.prototype.getOptions = function (options) {
debug('options', options);
var resultantOptions = objectAssignDeep({}, this.getDefaultOptions(), this.getCustomOptions(options));
Expand Down Expand Up @@ -97,7 +126,10 @@ MultiReporters.prototype.getCustomOptions = function (options) {
console.error(e);
throw e;
}
} else { customOptions = _.get(options, "reporterOptions"); }
}
else {
customOptions = _.get(options, "reporterOptions");
}

return customOptions;
};
Expand Down
54 changes: 54 additions & 0 deletions tests/lib/MultiReporters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,60 @@ describe('lib/MultiReporters', function () {
reporter = new mocha._reporter(runner, options);
});

describe('#done (failures, fn)', function () {
var failures, fn;

beforeEach(function () {
sinon.stub(console, 'error');
failures = 2;
fn = sinon.stub();
});

afterEach(function () {
console.error.restore();
});

it('logs an error message to the console when no reporters have been registered', function() {
reporter.done(failures, fn);

expect(fn.callCount).to.equal(0);
expect(console.error.callCount).to.equal(1);
expect(console.error.firstCall.args).to.deep.equal(['Unable to invoke fn(failures) - no reporters were registered']);
});

it('executes fn(failures) after applying the done method on each reporter', function() {
var reporterA = { done: sinon.stub().callsArg(1) };
var reporterB = {};
var reporterC = { done: sinon.stub().callsArg(1) };

reporter._reporters = [reporterA, reporterB, reporterC];

reporter.done(failures, fn);

expect(reporterA.done.callCount).to.equal(1);
expect(reporterA.done.firstCall.args[0]).to.equal(failures);
expect(typeof reporterA.done.firstCall.args[1]).to.equal('function');

expect(reporterC.done.callCount).to.equal(1);
expect(reporterC.done.firstCall.args[0]).to.equal(failures);
expect(typeof reporterC.done.firstCall.args[1]).to.equal('function');

expect(fn.callCount).to.equal(1);
expect(fn.calledAfter(reporterA.done)).to.be.true;
expect(fn.calledAfter(reporterC.done)).to.be.true;
expect(fn.firstCall.args).to.deep.equal([failures]);
});

it('executes fn(failures) when none of the registered reporters have a #done handlers', function () {
reporter._reporters = [{}, {}];

reporter.done(failures, fn);

expect(fn.callCount).to.equal(1);
expect(fn.firstCall.args).to.deep.equal([failures]);
});
});

describe('#options (reporters - single)', function () {
it('return reporter options: "dot"', function () {
expect(reporter.getReporterOptions(reporter.getOptions(options), 'dot')).to.be.deep.equal({
Expand Down

0 comments on commit 16cb351

Please sign in to comment.