From 90a3338085023d78a950ec1871d29ce61b1f26ba Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 12 May 2020 18:40:50 +0800 Subject: [PATCH] Array options (#67) * - Enhancement: Make it posible to supply `reporterEnabled` or `cmrOutput` as arrays; fixes #38 * - Maintenance: Add `.ncurc.js` to allow `npm-check-updates` to avoid updating peerDeps by default --- .ncurc.js | 8 +++++++ lib/MultiReporters.js | 19 ++++++++++++----- tests/custom-internal-config-array.json | 7 +++++++ tests/lib/MultiReporters.test.js | 28 +++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 .ncurc.js create mode 100644 tests/custom-internal-config-array.json diff --git a/.ncurc.js b/.ncurc.js new file mode 100644 index 0000000..0849ced --- /dev/null +++ b/.ncurc.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = { + // Whitelist all for checking besides `peer` which indicates + // somewhat older versions of `eslint` we still support even + // while our devDeps point to a more recent version + "dep": "prod,dev,optional,bundle" +}; diff --git a/lib/MultiReporters.js b/lib/MultiReporters.js index a080a88..748d380 100644 --- a/lib/MultiReporters.js +++ b/lib/MultiReporters.js @@ -46,7 +46,12 @@ function MultiReporters(runner, options) { if (_.get(options, 'execute', true)) { options = this.getOptions(options); - this._reporters = _.get(options, 'reporterEnabled', 'tap').split(',').map( + let enabledReporters = _.get(options, 'reporterEnabled', 'tap'); + if (!Array.isArray(enabledReporters)) { + enabledReporters = enabledReporters.split(','); + } + + this._reporters = enabledReporters.map( function processReporterEnabled(name, index) { debug(name, index); @@ -137,7 +142,7 @@ MultiReporters.prototype.getOptions = function (options) { const cmrOutput = _.get(options, 'reporterOptions.cmrOutput'); const resultantOptions = _.merge({}, this.getDefaultOptions(), this.getCustomOptions(options), cmrOutput ? {cmrOutput} : null); - + debug('options file (resultant)', resultantOptions); return resultantOptions; }; @@ -203,10 +208,14 @@ MultiReporters.prototype.getReporterOptions = function (options, name) { const resultantReporterOptions = _.merge({}, commonReporterOptions, customReporterOptions); debug('reporter options (resultant)', _name, resultantReporterOptions); - const cmrOutput = _.get(options, 'cmrOutput'); + let cmrOutput = _.get(options, 'cmrOutput'); if (cmrOutput) { - cmrOutput.split(':').forEach((cmrOutputReporter) => { - const [targetName, prop, output] = cmrOutputReporter.split('+'); + if (!Array.isArray(cmrOutput)) { + cmrOutput = cmrOutput.split(':').map((cmrOutputReporter) => { + return cmrOutputReporter.split('+'); + }); + } + cmrOutput.forEach(([targetName, prop, output]) => { if (resultantReporterOptions[prop] && _name === targetName) { resultantReporterOptions[prop] = resultantReporterOptions[prop].replace(/\{id\}/gu, output); } diff --git a/tests/custom-internal-config-array.json b/tests/custom-internal-config-array.json new file mode 100644 index 0000000..73cdfed --- /dev/null +++ b/tests/custom-internal-config-array.json @@ -0,0 +1,7 @@ +{ + "reporterEnabled": ["dot", "tests/custom-internal-reporter"], + + "xunitReporterOptions": { + "output": "artifacts/test/custom-xunit.xml" + } +} diff --git a/tests/lib/MultiReporters.test.js b/tests/lib/MultiReporters.test.js index c1300c1..7c05455 100644 --- a/tests/lib/MultiReporters.test.js +++ b/tests/lib/MultiReporters.test.js @@ -180,6 +180,24 @@ describe('lib/MultiReporters', function () { }); }); + describe('#custom-internal-reporter (array config)', function () { + beforeEach(function() { + options = { + execute: true, + reporterOptions: { + configFile: 'tests/custom-internal-config-array.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('#custom-erring-internal-reporter', function () { beforeEach(function() { options = { @@ -245,6 +263,16 @@ describe('lib/MultiReporters', function () { id: 'mocha-junit-reporter', mochaFile: 'junit.xml' }); + + expect(reporter.getReporterOptions(reporter.getOptions({ + reporterOptions: { + cmrOutput: ['tests/custom-internal-reporter', 'output', 'customName'], + configFile: 'tests/custom-internal-dynamic-output-config.json' + } + }), 'mocha-junit-reporter')).to.be.deep.equal({ + id: 'mocha-junit-reporter', + mochaFile: 'junit.xml' + }); }); });