Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify Mocha constructor to accept options.global or options.globals #3914

Merged
merged 9 commits into from Jun 6, 2019
14 changes: 11 additions & 3 deletions lib/mocha.js
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.
juergba marked this conversation as resolved.
Show resolved Hide resolved
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
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() {
juergba marked this conversation as resolved.
Show resolved Hide resolved
it('should pass options.global to #globals()', function() {
// eslint-disable-next-line no-new
juergba marked this conversation as resolved.
Show resolved Hide resolved
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
Member

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];
juergba marked this conversation as resolved.
Show resolved Hide resolved
mocha.globals(elems);
expect(mocha.options.globals, 'to contain', elem, elem2);
expect(mocha.options.globals, 'to have length', elems.length);
});
});
});

Expand Down