Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ 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. Globals are stored internally at this.options.globals.
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 +545,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 +556,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
47 changes: 47 additions & 0 deletions test/unit/mocha.spec.js
Original file line number Diff line number Diff line change
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not test the deletion, but it's ok ...

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 @@ -174,6 +213,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];
mocha.globals(elems);
expect(mocha.options.globals, 'to contain', elem, elem2);
expect(mocha.options.globals, 'to have length', elems.length);
});
});
});

Expand Down