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
Prev Previous commit
Next Next commit
consume options.global or options.globals in the constructor. filter …
…unique values
pascalpp committed May 29, 2019
commit 9b8b611849f1800f6e1dbbe630e1cd4cc8862d48
14 changes: 11 additions & 3 deletions lib/mocha.js
Original file line number Diff line number Diff line change
@@ -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)
@@ -114,7 +119,7 @@ function Mocha(options) {
.useColors(options.color)
.slow(options.slow)
.useInlineDiffs(options.inlineDiffs)
.globals(options.global || options.globals);
.globals(options.globals);

if ('enableTimeouts' in options) {
utils.deprecate(
@@ -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;
};

16 changes: 16 additions & 0 deletions test/unit/mocha.spec.js
Original file line number Diff line number Diff line change
@@ -74,6 +74,10 @@ describe('Mocha', function() {
['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() {
@@ -94,6 +98,10 @@ describe('Mocha', function() {
['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);
});
});
});

@@ -205,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);
});
});
});