Skip to content

Commit

Permalink
Allow for custom options via Js file (#27)
Browse files Browse the repository at this point in the history
* allow for custom options via Js file
  • Loading branch information
hiddentao authored and stanleyhlng committed Dec 20, 2016
1 parent 33b3f28 commit cbcb0f5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 22 deletions.
12 changes: 8 additions & 4 deletions lib/MultiReporters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions tests/custom-external-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
"reporterEnabled": "mocha-junit-reporter",

"mochaJunitReporterReporterOptions": {
"id": "mocha-junit-reporter",
"mochaFile": "junit.xml"
}
}
71 changes: 53 additions & 18 deletions tests/lib/MultiReporters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
});
});
});
Expand All @@ -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);
});
Expand All @@ -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 () {
Expand Down

0 comments on commit cbcb0f5

Please sign in to comment.