Skip to content

Commit

Permalink
modify Mocha constructor to accept options.global or options.globals (#…
Browse files Browse the repository at this point in the history
…3914)

* consume options.global or options.globals in the constructor. filter unique values

* added tests for options.global and options.globals

* fixed API doc link
  • Loading branch information
pascalpp authored and juergba committed Jun 6, 2019
1 parent ca861d4 commit d02a096
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/mocha.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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;
};

Expand Down
48 changes: 48 additions & 0 deletions test/unit/mocha.spec.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
});
});

Expand Down

0 comments on commit d02a096

Please sign in to comment.