From cbcb0f5012f86dd4d49ad98748ba64454589e252 Mon Sep 17 00:00:00 2001 From: Ramesh Nair Date: Tue, 20 Dec 2016 08:03:47 +0000 Subject: [PATCH] Allow for custom options via Js file (#27) * allow for custom options via Js file --- lib/MultiReporters.js | 12 ++++-- tests/custom-external-config.js | 8 ++++ tests/lib/MultiReporters.test.js | 71 ++++++++++++++++++++++++-------- 3 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 tests/custom-external-config.js diff --git a/lib/MultiReporters.js b/lib/MultiReporters.js index c82f8ff..f83d765 100644 --- a/lib/MultiReporters.js +++ b/lib/MultiReporters.js @@ -83,11 +83,15 @@ MultiReporters.prototype.getCustomOptions = function (options) { customOptionsFile = path.resolve(customOptionsFile); debug('options file (custom)', customOptionsFile); - customOptions = fs.readFileSync(customOptionsFile).toString(); - debug('options (custom)', customOptions); - try { - customOptions = JSON.parse(customOptions); + if ('.js' === path.extname(customOptionsFile)) { + customOptions = require(customOptionsFile); + } + else { + customOptions = JSON.parse(fs.readFileSync(customOptionsFile).toString()); + } + + debug('options (custom)', customOptions); } catch (e) { console.error(e); diff --git a/tests/custom-external-config.js b/tests/custom-external-config.js new file mode 100644 index 0000000..056e120 --- /dev/null +++ b/tests/custom-external-config.js @@ -0,0 +1,8 @@ +module.exports = { + "reporterEnabled": "mocha-junit-reporter", + + "mochaJunitReporterReporterOptions": { + "id": "mocha-junit-reporter", + "mochaFile": "junit.xml" + } +} diff --git a/tests/lib/MultiReporters.test.js b/tests/lib/MultiReporters.test.js index cc47f58..0600d3b 100644 --- a/tests/lib/MultiReporters.test.js +++ b/tests/lib/MultiReporters.test.js @@ -110,26 +110,54 @@ describe('lib/MultiReporters', function () { }); describe('#external', function () { - beforeEach(function () { - var mocha = new Mocha({ - reporter: MultiReporters + describe('json', 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); + }); + + 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' + }); + }); }); - 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); }); - 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' + describe('js', 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.js' + } + }; + reporter = new mocha._reporter(runner, options); + }); + + 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' + }); }); }); }); @@ -138,6 +166,13 @@ describe('lib/MultiReporters', function () { describe('#exception', function () { var err; beforeEach(function () { + options = { + execute: false, + reporterOptions: { + configFile: 'tests/custom-external-config.json' + } + }; + err = new Error('JSON.parse error!'); sinon.stub(JSON, 'parse').throws(err); }); @@ -159,7 +194,7 @@ describe('lib/MultiReporters', function () { expect(JSON.parse.threw()).to.equal(true); expect(JSON.parse.callCount).to.equal(1); }); - }); + }); }); describe('#test', function () {