diff --git a/lib/mocha.js b/lib/mocha.js index aab5604448..c0a5aa6dd9 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -106,6 +106,10 @@ function Mocha(options) { options.color = 'color' in options ? options.color : options.useColors; } + // Globals are passed in as options.global, with options.globals for backward compatibility. + options.globals = options.global || options.globals || []; + delete options.global; + this.grep(options.grep) .fgrep(options.fgrep) .ui(options.ui) @@ -540,7 +544,7 @@ Mocha.prototype._growl = growl.notify; * Specifies whitelist of variable names to be expected in global scope. * * @public - * @see {@link https://mochajs.org/#--globals-names|CLI option} + * @see {@link https://mochajs.org/#-global-variable-name|CLI option} * @see {@link Mocha#checkLeaks} * @param {String[]|String} globals - Accepted global variable name(s). * @return {Mocha} this @@ -551,9 +555,12 @@ Mocha.prototype._growl = growl.notify; * mocha.globals(['jQuery', 'MyLib']); */ Mocha.prototype.globals = function(globals) { - this.options.globals = (this.options.globals || []) + this.options.globals = this.options.globals .concat(globals) - .filter(Boolean); + .filter(Boolean) + .filter(function(elt, idx, arr) { + return arr.indexOf(elt) === idx; + }); return this; }; diff --git a/test/unit/mocha.spec.js b/test/unit/mocha.spec.js index 16fbbd7f82..ed99a3e257 100644 --- a/test/unit/mocha.spec.js +++ b/test/unit/mocha.spec.js @@ -21,6 +21,7 @@ describe('Mocha', function() { sandbox.stub(Mocha.prototype, 'useColors').returnsThis(); sandbox.stub(utils, 'deprecate'); sandbox.stub(Mocha.prototype, 'timeout').returnsThis(); + sandbox.stub(Mocha.prototype, 'globals').returnsThis(); }); describe('when "useColors" option is defined', function() { @@ -64,6 +65,44 @@ describe('Mocha', function() { ); }); }); + + describe('when "options.global" is provided', function() { + it('should pass "options.global" to #globals()', function() { + // eslint-disable-next-line no-new + new Mocha({global: ['singular']}); + expect(Mocha.prototype.globals, 'to have a call satisfying', [ + ['singular'] + ]).and('was called once'); + }); + it('should delete mocha.options.global', function() { + var mocha = new Mocha({global: ['singular']}); + expect(mocha.options.global, 'to be', undefined); + }); + }); + + describe('when "options.globals" is provided', function() { + it('should pass "options.globals" to #globals()', function() { + // eslint-disable-next-line no-new + new Mocha({globals: ['plural']}); + expect(Mocha.prototype.globals, 'to have a call satisfying', [ + ['plural'] + ]).and('was called once'); + }); + }); + + describe('when "options.global" AND "options.globals" are provided', function() { + it('should pass "options.global" to #globals(), ignoring "options.globals"', function() { + // eslint-disable-next-line no-new + new Mocha({global: ['singular'], globals: ['plural']}); + expect(Mocha.prototype.globals, 'to have a call satisfying', [ + ['singular'] + ]).and('was called once'); + }); + it('should delete mocha.options.global', function() { + var mocha = new Mocha({global: ['singular'], globals: ['plural']}); + expect(mocha.options.global, 'to be', undefined); + }); + }); }); describe('#allowUncaught()', function() { @@ -159,6 +198,7 @@ describe('Mocha', function() { describe('when argument is valid', function() { var elem = 'foo'; var elem2 = 'bar'; + var elem3 = 'baz'; it('should add string to the whitelist', function() { var mocha = new Mocha(opts); @@ -174,6 +214,14 @@ describe('Mocha', function() { expect(mocha.options.globals, 'to contain', elem, elem2); expect(mocha.options.globals, 'to have length', elems.length); }); + + it('should not have duplicates', function() { + var mocha = new Mocha({globals: [elem, elem2]}); + var elems = [elem, elem2, elem3]; + mocha.globals(elems); + expect(mocha.options.globals, 'to contain', elem, elem2, elem3); + expect(mocha.options.globals, 'to have length', elems.length); + }); }); });