Skip to content

Commit 82307fb

Browse files
authored
Fix .globals to remove falsy values (#3737)
* Removed falsy values from `.globals` using filter for "undefined"/empty string values. * Added tests for `.globals` * Reordered "mocha.spec.js" option tests lexically * Changed `describe` titles from `.method` to `#method` for Mocha prototypes. * Renamed suite title 'error handling' as '#reporter()' * Reparented '#reporter("xunit")#run(fn)' test suite inside '#run()' test suite.
1 parent 56dc28e commit 82307fb

File tree

2 files changed

+150
-96
lines changed

2 files changed

+150
-96
lines changed

lib/mocha.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,9 @@ Mocha.prototype._growl = growl.notify;
543543
* mocha.globals(['jQuery', 'MyLib']);
544544
*/
545545
Mocha.prototype.globals = function(globals) {
546-
this.options.globals = (this.options.globals || []).concat(globals);
546+
this.options.globals = (this.options.globals || [])
547+
.concat(globals)
548+
.filter(Boolean);
547549
return this;
548550
};
549551

test/unit/mocha.spec.js

+147-95
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var Mocha = require('../../lib/mocha');
55
var sinon = require('sinon');
66

77
describe('Mocha', function() {
8-
var opts = {reporter: function() {}}; // no output
8+
var opts = {reporter: utils.noop}; // no output
99
var sandbox;
1010

1111
beforeEach(function() {
@@ -41,88 +41,59 @@ describe('Mocha', function() {
4141
});
4242
});
4343

44-
describe('.run(fn)', function() {
45-
it('should not raise errors if callback was not provided', function() {
46-
sandbox.stub(Mocha.Runner.prototype, 'run');
44+
describe('#allowUncaught()', function() {
45+
it('should set the allowUncaught option to true', function() {
4746
var mocha = new Mocha(opts);
48-
expect(function() {
49-
mocha.run();
50-
}, 'not to throw');
47+
mocha.allowUncaught();
48+
expect(mocha.options, 'to have property', 'allowUncaught', true);
5149
});
5250

53-
it('should execute the callback when complete', function(done) {
51+
it('should be chainable', function() {
5452
var mocha = new Mocha(opts);
55-
sandbox.stub(Mocha.Runner.prototype, 'run').callsArg(0);
56-
mocha.run(done);
57-
});
58-
});
59-
60-
describe('.reporter("xunit").run(fn)', function() {
61-
it('should not raise errors if callback was not provided', function() {
62-
var mocha = new Mocha();
63-
expect(function() {
64-
try {
65-
mocha.reporter('xunit').run();
66-
} catch (e) {
67-
console.log(e);
68-
expect.fail(e.message);
69-
}
70-
}, 'not to throw');
53+
expect(mocha.allowUncaught(), 'to be', mocha);
7154
});
7255
});
7356

74-
describe('.invert()', function() {
75-
it('should set the invert option to true', function() {
57+
describe('#bail()', function() {
58+
it('should set the suite._bail to true if there is no arguments', function() {
7659
var mocha = new Mocha(opts);
77-
mocha.invert();
78-
expect(mocha.options, 'to have property', 'invert', true);
60+
mocha.bail();
61+
expect(mocha.suite._bail, 'to be', true);
7962
});
8063

8164
it('should be chainable', function() {
8265
var mocha = new Mocha(opts);
83-
expect(mocha.invert(), 'to be', mocha);
66+
expect(mocha.bail(), 'to be', mocha);
8467
});
8568
});
8669

87-
describe('.ignoreLeaks()', function() {
88-
it('should set the ignoreLeaks option to true when param equals true', function() {
89-
var mocha = new Mocha(opts);
90-
mocha.ignoreLeaks(true);
91-
expect(mocha.options, 'to have property', 'ignoreLeaks', true);
92-
});
93-
94-
it('should set the ignoreLeaks option to false when param equals false', function() {
95-
var mocha = new Mocha(opts);
96-
mocha.ignoreLeaks(false);
97-
expect(mocha.options, 'to have property', 'ignoreLeaks', false);
98-
});
99-
100-
it('should set the ignoreLeaks option to false when the param is undefined', function() {
70+
describe('#checkLeaks()', function() {
71+
it('should set the ignoreLeaks option to false', function() {
10172
var mocha = new Mocha(opts);
102-
mocha.ignoreLeaks();
73+
mocha.checkLeaks();
10374
expect(mocha.options, 'to have property', 'ignoreLeaks', false);
10475
});
10576

10677
it('should be chainable', function() {
10778
var mocha = new Mocha(opts);
108-
expect(mocha.ignoreLeaks(), 'to be', mocha);
79+
expect(mocha.checkLeaks(), 'to be', mocha);
10980
});
11081
});
11182

112-
describe('.checkLeaks()', function() {
113-
it('should set the ignoreLeaks option to false', function() {
83+
describe('#delay()', function() {
84+
it('should set the delay option to true', function() {
11485
var mocha = new Mocha(opts);
115-
mocha.checkLeaks();
116-
expect(mocha.options, 'to have property', 'ignoreLeaks', false);
86+
mocha.delay();
87+
expect(mocha.options, 'to have property', 'delay', true);
11788
});
11889

11990
it('should be chainable', function() {
12091
var mocha = new Mocha(opts);
121-
expect(mocha.checkLeaks(), 'to be', mocha);
92+
expect(mocha.delay(), 'to be', mocha);
12293
});
12394
});
12495

125-
describe('.fullTrace()', function() {
96+
describe('#fullTrace()', function() {
12697
it('should set the fullStackTrace option to true', function() {
12798
var mocha = new Mocha(opts);
12899
mocha.fullTrace();
@@ -135,7 +106,53 @@ describe('Mocha', function() {
135106
});
136107
});
137108

138-
describe('.growl()', function() {
109+
describe('#globals()', function() {
110+
it('should be an empty array initially', function() {
111+
var mocha = new Mocha();
112+
expect(mocha.options.globals, 'to be empty');
113+
});
114+
115+
it('should be chainable', function() {
116+
var mocha = new Mocha(opts);
117+
expect(mocha.globals(), 'to be', mocha);
118+
});
119+
120+
describe('when argument is invalid', function() {
121+
it('should not modify the whitelist when given empty string', function() {
122+
var mocha = new Mocha(opts);
123+
mocha.globals('');
124+
expect(mocha.options.globals, 'to be empty');
125+
});
126+
127+
it('should not modify the whitelist when given empty array', function() {
128+
var mocha = new Mocha(opts);
129+
mocha.globals([]);
130+
expect(mocha.options.globals, 'to be empty');
131+
});
132+
});
133+
134+
describe('when argument is valid', function() {
135+
var elem = 'foo';
136+
var elem2 = 'bar';
137+
138+
it('should add string to the whitelist', function() {
139+
var mocha = new Mocha(opts);
140+
mocha.globals(elem);
141+
expect(mocha.options.globals, 'to contain', elem);
142+
expect(mocha.options.globals, 'to have length', 1);
143+
});
144+
145+
it('should add contents of string array to the whitelist', function() {
146+
var mocha = new Mocha(opts);
147+
var elems = [elem, elem2];
148+
mocha.globals(elems);
149+
expect(mocha.options.globals, 'to contain', elem, elem2);
150+
expect(mocha.options.globals, 'to have length', elems.length);
151+
});
152+
});
153+
});
154+
155+
describe('#growl()', function() {
139156
describe('if capable of notifications', function() {
140157
it('should set the growl option to true', function() {
141158
var mocha = new Mocha(opts);
@@ -164,32 +181,45 @@ describe('Mocha', function() {
164181
});
165182
});
166183

167-
describe('.useInlineDiffs()', function() {
168-
it('should set the useInlineDiffs option to true when param equals true', function() {
184+
describe('#ignoreLeaks()', function() {
185+
it('should set the ignoreLeaks option to true when param equals true', function() {
169186
var mocha = new Mocha(opts);
170-
mocha.useInlineDiffs(true);
171-
expect(mocha.options, 'to have property', 'useInlineDiffs', true);
187+
mocha.ignoreLeaks(true);
188+
expect(mocha.options, 'to have property', 'ignoreLeaks', true);
172189
});
173190

174-
it('should set the useInlineDiffs option to false when param equals false', function() {
191+
it('should set the ignoreLeaks option to false when param equals false', function() {
175192
var mocha = new Mocha(opts);
176-
mocha.useInlineDiffs(false);
177-
expect(mocha.options, 'to have property', 'useInlineDiffs', false);
193+
mocha.ignoreLeaks(false);
194+
expect(mocha.options, 'to have property', 'ignoreLeaks', false);
178195
});
179196

180-
it('should set the useInlineDiffs option to false when the param is undefined', function() {
197+
it('should set the ignoreLeaks option to false when the param is undefined', function() {
181198
var mocha = new Mocha(opts);
182-
mocha.useInlineDiffs();
183-
expect(mocha.options, 'to have property', 'useInlineDiffs', false);
199+
mocha.ignoreLeaks();
200+
expect(mocha.options, 'to have property', 'ignoreLeaks', false);
184201
});
185202

186203
it('should be chainable', function() {
187204
var mocha = new Mocha(opts);
188-
expect(mocha.useInlineDiffs(), 'to be', mocha);
205+
expect(mocha.ignoreLeaks(), 'to be', mocha);
206+
});
207+
});
208+
209+
describe('#invert()', function() {
210+
it('should set the invert option to true', function() {
211+
var mocha = new Mocha(opts);
212+
mocha.invert();
213+
expect(mocha.options, 'to have property', 'invert', true);
214+
});
215+
216+
it('should be chainable', function() {
217+
var mocha = new Mocha(opts);
218+
expect(mocha.invert(), 'to be', mocha);
189219
});
190220
});
191221

192-
describe('.noHighlighting()', function() {
222+
describe('#noHighlighting()', function() {
193223
// :NOTE: Browser-only option...
194224
it('should set the noHighlighting option to true', function() {
195225
var mocha = new Mocha(opts);
@@ -203,57 +233,79 @@ describe('Mocha', function() {
203233
});
204234
});
205235

206-
describe('.allowUncaught()', function() {
207-
it('should set the allowUncaught option to true', function() {
208-
var mocha = new Mocha(opts);
209-
mocha.allowUncaught();
210-
expect(mocha.options, 'to have property', 'allowUncaught', true);
236+
describe('#reporter()', function() {
237+
it('should throw reporter error if an invalid reporter is given', function() {
238+
var updatedOpts = {reporter: 'invalidReporter', reporterOptions: {}};
239+
var throwError = function() {
240+
// eslint-disable-next-line no-new
241+
new Mocha(updatedOpts);
242+
};
243+
expect(throwError, 'to throw', {
244+
message: "invalid reporter 'invalidReporter'",
245+
code: 'ERR_MOCHA_INVALID_REPORTER',
246+
reporter: 'invalidReporter'
247+
});
211248
});
212249

213250
it('should be chainable', function() {
214251
var mocha = new Mocha(opts);
215-
expect(mocha.allowUncaught(), 'to be', mocha);
252+
expect(mocha.reporter(), 'to be', mocha);
216253
});
217254
});
218255

219-
describe('.delay()', function() {
220-
it('should set the delay option to true', function() {
256+
describe('#run(fn)', function() {
257+
it('should execute the callback when complete', function(done) {
221258
var mocha = new Mocha(opts);
222-
mocha.delay();
223-
expect(mocha.options, 'to have property', 'delay', true);
259+
sandbox.stub(Mocha.Runner.prototype, 'run').callsArg(0);
260+
mocha.run(done);
224261
});
225262

226-
it('should be chainable', function() {
263+
it('should not raise errors if callback was not provided', function() {
264+
sandbox.stub(Mocha.Runner.prototype, 'run');
227265
var mocha = new Mocha(opts);
228-
expect(mocha.delay(), 'to be', mocha);
266+
expect(function() {
267+
mocha.run();
268+
}, 'not to throw');
269+
});
270+
271+
describe('#reporter("xunit")#run(fn)', function() {
272+
// :TBD: Why does specifying reporter differentiate this test from preceding one
273+
it('should not raise errors if callback was not provided', function() {
274+
var mocha = new Mocha();
275+
expect(function() {
276+
try {
277+
mocha.reporter('xunit').run();
278+
} catch (e) {
279+
console.log(e);
280+
expect.fail(e.message);
281+
}
282+
}, 'not to throw');
283+
});
229284
});
230285
});
231286

232-
describe('.bail()', function() {
233-
it('should set the suite._bail to true if there is no arguments', function() {
287+
describe('#useInlineDiffs()', function() {
288+
it('should set the useInlineDiffs option to true when param equals true', function() {
234289
var mocha = new Mocha(opts);
235-
mocha.bail();
236-
expect(mocha.suite._bail, 'to be', true);
290+
mocha.useInlineDiffs(true);
291+
expect(mocha.options, 'to have property', 'useInlineDiffs', true);
237292
});
238293

239-
it('should be chainable', function() {
294+
it('should set the useInlineDiffs option to false when param equals false', function() {
240295
var mocha = new Mocha(opts);
241-
expect(mocha.bail(), 'to be', mocha);
296+
mocha.useInlineDiffs(false);
297+
expect(mocha.options, 'to have property', 'useInlineDiffs', false);
242298
});
243-
});
244299

245-
describe('error handling', function() {
246-
it('should throw reporter error if an invalid reporter is given', function() {
247-
var updatedOpts = {reporter: 'invalidReporter', reporterOptions: {}};
248-
var throwError = function() {
249-
// eslint-disable-next-line no-new
250-
new Mocha(updatedOpts);
251-
};
252-
expect(throwError, 'to throw', {
253-
message: "invalid reporter 'invalidReporter'",
254-
code: 'ERR_MOCHA_INVALID_REPORTER',
255-
reporter: 'invalidReporter'
256-
});
300+
it('should set the useInlineDiffs option to false when the param is undefined', function() {
301+
var mocha = new Mocha(opts);
302+
mocha.useInlineDiffs();
303+
expect(mocha.options, 'to have property', 'useInlineDiffs', false);
304+
});
305+
306+
it('should be chainable', function() {
307+
var mocha = new Mocha(opts);
308+
expect(mocha.useInlineDiffs(), 'to be', mocha);
257309
});
258310
});
259311
});

0 commit comments

Comments
 (0)