Skip to content

Commit d02a096

Browse files
pascalppjuergba
authored andcommitted
modify Mocha constructor to accept options.global or options.globals (#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
1 parent ca861d4 commit d02a096

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

lib/mocha.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ function Mocha(options) {
106106
options.color = 'color' in options ? options.color : options.useColors;
107107
}
108108

109+
// Globals are passed in as options.global, with options.globals for backward compatibility.
110+
options.globals = options.global || options.globals || [];
111+
delete options.global;
112+
109113
this.grep(options.grep)
110114
.fgrep(options.fgrep)
111115
.ui(options.ui)
@@ -540,7 +544,7 @@ Mocha.prototype._growl = growl.notify;
540544
* Specifies whitelist of variable names to be expected in global scope.
541545
*
542546
* @public
543-
* @see {@link https://mochajs.org/#--globals-names|CLI option}
547+
* @see {@link https://mochajs.org/#-global-variable-name|CLI option}
544548
* @see {@link Mocha#checkLeaks}
545549
* @param {String[]|String} globals - Accepted global variable name(s).
546550
* @return {Mocha} this
@@ -551,9 +555,12 @@ Mocha.prototype._growl = growl.notify;
551555
* mocha.globals(['jQuery', 'MyLib']);
552556
*/
553557
Mocha.prototype.globals = function(globals) {
554-
this.options.globals = (this.options.globals || [])
558+
this.options.globals = this.options.globals
555559
.concat(globals)
556-
.filter(Boolean);
560+
.filter(Boolean)
561+
.filter(function(elt, idx, arr) {
562+
return arr.indexOf(elt) === idx;
563+
});
557564
return this;
558565
};
559566

test/unit/mocha.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('Mocha', function() {
2121
sandbox.stub(Mocha.prototype, 'useColors').returnsThis();
2222
sandbox.stub(utils, 'deprecate');
2323
sandbox.stub(Mocha.prototype, 'timeout').returnsThis();
24+
sandbox.stub(Mocha.prototype, 'globals').returnsThis();
2425
});
2526

2627
describe('when "useColors" option is defined', function() {
@@ -64,6 +65,44 @@ describe('Mocha', function() {
6465
);
6566
});
6667
});
68+
69+
describe('when "options.global" is provided', function() {
70+
it('should pass "options.global" to #globals()', function() {
71+
// eslint-disable-next-line no-new
72+
new Mocha({global: ['singular']});
73+
expect(Mocha.prototype.globals, 'to have a call satisfying', [
74+
['singular']
75+
]).and('was called once');
76+
});
77+
it('should delete mocha.options.global', function() {
78+
var mocha = new Mocha({global: ['singular']});
79+
expect(mocha.options.global, 'to be', undefined);
80+
});
81+
});
82+
83+
describe('when "options.globals" is provided', function() {
84+
it('should pass "options.globals" to #globals()', function() {
85+
// eslint-disable-next-line no-new
86+
new Mocha({globals: ['plural']});
87+
expect(Mocha.prototype.globals, 'to have a call satisfying', [
88+
['plural']
89+
]).and('was called once');
90+
});
91+
});
92+
93+
describe('when "options.global" AND "options.globals" are provided', function() {
94+
it('should pass "options.global" to #globals(), ignoring "options.globals"', function() {
95+
// eslint-disable-next-line no-new
96+
new Mocha({global: ['singular'], globals: ['plural']});
97+
expect(Mocha.prototype.globals, 'to have a call satisfying', [
98+
['singular']
99+
]).and('was called once');
100+
});
101+
it('should delete mocha.options.global', function() {
102+
var mocha = new Mocha({global: ['singular'], globals: ['plural']});
103+
expect(mocha.options.global, 'to be', undefined);
104+
});
105+
});
67106
});
68107

69108
describe('#allowUncaught()', function() {
@@ -159,6 +198,7 @@ describe('Mocha', function() {
159198
describe('when argument is valid', function() {
160199
var elem = 'foo';
161200
var elem2 = 'bar';
201+
var elem3 = 'baz';
162202

163203
it('should add string to the whitelist', function() {
164204
var mocha = new Mocha(opts);
@@ -174,6 +214,14 @@ describe('Mocha', function() {
174214
expect(mocha.options.globals, 'to contain', elem, elem2);
175215
expect(mocha.options.globals, 'to have length', elems.length);
176216
});
217+
218+
it('should not have duplicates', function() {
219+
var mocha = new Mocha({globals: [elem, elem2]});
220+
var elems = [elem, elem2, elem3];
221+
mocha.globals(elems);
222+
expect(mocha.options.globals, 'to contain', elem, elem2, elem3);
223+
expect(mocha.options.globals, 'to have length', elems.length);
224+
});
177225
});
178226
});
179227

0 commit comments

Comments
 (0)