diff --git a/lib/MultiReporters.js b/lib/MultiReporters.js index f33a55a..b587c40 100644 --- a/lib/MultiReporters.js +++ b/lib/MultiReporters.js @@ -5,7 +5,8 @@ var _ = { camelCase: require('lodash/camelCase'), size: require('lodash/size'), after: require('lodash/after'), - merge: require('lodash/merge') + merge: require('lodash/merge'), + isFunction: require('lodash/isFunction') }; var debug = require('debug')('mocha:reporters:MultiReporters'); var fs = require('fs'); @@ -18,11 +19,26 @@ function MultiReporters(runner, options) { Base.call(this, runner); if (_.get(options, 'execute', true)) { - options = this.getOptions(options); - this._reporters = _.get(options, 'reporterEnabled', 'tap').split(',').map( + var reportersToMap; + if (_.get(options, 'reporterEnabled', ['tap']).constructor === Array) { + reportersToMap = _.get(options, 'reporterEnabled', ['tap']) + } + else { + reportersToMap = _.get(options, 'reporterEnabled', 'tap').split(',') + } + + this._reporters = reportersToMap.map( function processReporterEnabled(name, index) { + // + // Reporter is a function(runner, option) + if (_.isFunction(name)) { + var reporterOptions = this.getReporterOptions(options, name.name) + return name(runner, {reporterOptions: reporterOptions}) + } + + // debug(name, index); name = name.trim(); @@ -68,7 +84,7 @@ function MultiReporters(runner, options) { ); } else { - console.error('Reporter does not found!', name); + console.error('Reporter was not found!', name); } }.bind(this) ); diff --git a/tests/custom-array-config.json b/tests/custom-array-config.json new file mode 100644 index 0000000..73cdfed --- /dev/null +++ b/tests/custom-array-config.json @@ -0,0 +1,7 @@ +{ + "reporterEnabled": ["dot", "tests/custom-internal-reporter"], + + "xunitReporterOptions": { + "output": "artifacts/test/custom-xunit.xml" + } +} diff --git a/tests/custom-array-function-config.js b/tests/custom-array-function-config.js new file mode 100644 index 0000000..6e38009 --- /dev/null +++ b/tests/custom-array-function-config.js @@ -0,0 +1,8 @@ +var customInternalReporter = require("tests/custom-internal-reporter") +module.exports = { + "reporterEnabled": ["dot", customInternalReporter], + + "xunitReporterOptions": { + "output": "artifacts/test/custom-xunit.xml" + } +} diff --git a/tests/lib/MultiReporters.test.js b/tests/lib/MultiReporters.test.js index bc5ceb4..eeb0494 100644 --- a/tests/lib/MultiReporters.test.js +++ b/tests/lib/MultiReporters.test.js @@ -29,6 +29,80 @@ describe('lib/MultiReporters', function () { var reporter; var options; + describe('#array-function', function() { + beforeEach(function () { + mocha = new Mocha({ + reporter: MultiReporters + }); + suite = new Suite('#internal-multi-reporter', 'root'); + runner = new Runner(suite); + options = { + execute: false, + reporterOptions: { + configFile: 'tests/custom-array-config.json' + } + }; + reporter = new mocha._reporter(runner, options); + }); + + it('return options from an array of string names', function () { + expect(reporter.getOptions(options)).to.be.deep.equal({ + reporterEnabled: ['dot', 'tests/custom-internal-reporter'], + reporterOptions: { + id: 'default' + }, + dotReporterOptions: { + id: 'dot' + }, + xunitReporterOptions: { + id: 'xunit', + output: 'artifacts/test/custom-xunit.xml' + }, + tapReporterOptions: { + id: 'tap' + } + }); + }) + }) + + describe('#array-string', function() { + beforeEach(function () { + mocha = new Mocha({ + reporter: MultiReporters + }); + suite = new Suite('#internal-multi-reporter', 'root'); + runner = new Runner(suite); + options = { + execute: false, + reporterOptions: { + configFile: 'tests/custom-array-config.json' + } + }; + reporter = new mocha._reporter(runner, options); + }); + + it('return options from an array of string names', function () { + expect(reporter.getOptions(options)).to.be.deep.equal({ + reporterEnabled: ['dot', 'tests/custom-internal-reporter'], + reporterOptions: { + id: 'default' + }, + dotReporterOptions: { + id: 'dot' + }, + xunitReporterOptions: { + id: 'xunit', + output: 'artifacts/test/custom-xunit.xml' + }, + tapReporterOptions: { + id: 'tap' + } + }); + }) + }) + + + describe('#internal', function () { beforeEach(function () { mocha = new Mocha({