/
mocha.spec.js
654 lines (571 loc) · 20.4 KB
/
mocha.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
'use strict';
var utils = require('../../lib/utils');
var Mocha = require('../../lib/mocha');
var sinon = require('sinon');
describe('Mocha', function() {
var opts = {reporter: utils.noop}; // no output
var sandbox;
beforeEach(function() {
sandbox = sinon.createSandbox();
});
afterEach(function() {
sandbox.restore();
});
describe('constructor', function() {
var mocha;
beforeEach(function() {
mocha = sandbox.createStubInstance(Mocha);
mocha.timeout.returnsThis();
mocha.retries.returnsThis();
sandbox.stub(Mocha.prototype, 'timeout').returnsThis();
sandbox.stub(Mocha.prototype, 'global').returnsThis();
sandbox.stub(Mocha.prototype, 'retries').returnsThis();
sandbox.stub(Mocha.prototype, 'rootHooks').returnsThis();
});
it('should set _cleanReferencesAfterRun to true', function() {
expect(new Mocha()._cleanReferencesAfterRun, 'to be', true);
});
describe('when "options.timeout" is `undefined`', function() {
it('should not attempt to set timeout', function() {
// eslint-disable-next-line no-new
new Mocha({timeout: undefined});
expect(Mocha.prototype.timeout, 'was not called');
});
});
describe('when `timeout` option is `false`', function() {
it('should attempt to set timeout', function() {
// eslint-disable-next-line no-new
new Mocha({timeout: false});
expect(Mocha.prototype.timeout, 'to have a call satisfying', [0]).and(
'was called once'
);
});
});
describe('when `global` option is an `Array`', function() {
it('should attempt to set globals', function() {
// eslint-disable-next-line no-new
new Mocha({global: ['singular']});
expect(Mocha.prototype.global, 'to have a call satisfying', [
['singular']
]).and('was called once');
});
});
describe('when `retries` option is present', function() {
it('should attempt to set retries`', function() {
// eslint-disable-next-line no-new
new Mocha({retries: 1});
expect(Mocha.prototype.retries, 'to have a call satisfying', [1]).and(
'was called once'
);
});
});
describe('when `retries` option is not present', function() {
it('should not attempt to set retries', function() {
// eslint-disable-next-line no-new
new Mocha({});
expect(Mocha.prototype.retries, 'was not called');
});
});
describe('when `rootHooks` option is truthy', function() {
it('shouid attempt to set root hooks', function() {
// eslint-disable-next-line no-new
new Mocha({rootHooks: ['a root hook']});
expect(Mocha.prototype.rootHooks, 'to have a call satisfying', [
['a root hook']
]).and('was called once');
});
});
});
describe('#allowUncaught()', function() {
it('should set the allowUncaught option to true', function() {
var mocha = new Mocha(opts);
mocha.allowUncaught();
expect(mocha.options, 'to have property', 'allowUncaught', true);
});
it('should set the allowUncaught option to false', function() {
var mocha = new Mocha(opts);
mocha.allowUncaught(false);
expect(mocha.options, 'to have property', 'allowUncaught', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.allowUncaught(), 'to be', mocha);
});
});
describe('#asyncOnly()', function() {
it('should set the asyncOnly option to true', function() {
var mocha = new Mocha(opts);
mocha.asyncOnly();
expect(mocha.options, 'to have property', 'asyncOnly', true);
});
it('should set the asyncOnly option to false', function() {
var mocha = new Mocha(opts);
mocha.asyncOnly(false);
expect(mocha.options, 'to have property', 'asyncOnly', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.asyncOnly(), 'to be', mocha);
});
});
describe('#bail()', function() {
it('should set the suite._bail to true if there is no arguments', function() {
var mocha = new Mocha(opts);
mocha.bail();
expect(mocha.suite._bail, 'to be', true);
});
it('should set the suite._bail to false', function() {
var mocha = new Mocha(opts);
mocha.bail(false);
expect(mocha.suite._bail, 'to be', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.bail(), 'to be', mocha);
});
});
describe('#checkLeaks()', function() {
it('should set the checkLeaks option to true', function() {
var mocha = new Mocha(opts);
mocha.checkLeaks();
expect(mocha.options, 'to have property', 'checkLeaks', true);
});
it('should set the checkLeaks option to false', function() {
var mocha = new Mocha(opts);
mocha.checkLeaks(false);
expect(mocha.options, 'to have property', 'checkLeaks', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.checkLeaks(), 'to be', mocha);
});
});
describe('#cleanReferencesAfterRun()', function() {
it('should set the _cleanReferencesAfterRun attribute', function() {
var mocha = new Mocha(opts);
mocha.cleanReferencesAfterRun();
expect(mocha._cleanReferencesAfterRun, 'to be', true);
});
it('should set the _cleanReferencesAfterRun attribute to false', function() {
var mocha = new Mocha(opts);
mocha.cleanReferencesAfterRun(false);
expect(mocha._cleanReferencesAfterRun, 'to be', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.cleanReferencesAfterRun(), 'to be', mocha);
});
});
describe('#color()', function() {
it('should set the color option to true', function() {
var mocha = new Mocha(opts);
mocha.color();
expect(mocha.options, 'to have property', 'color', true);
});
it('should set the color option to false', function() {
var mocha = new Mocha(opts);
mocha.color(false);
expect(mocha.options, 'to have property', 'color', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.color(), 'to be', mocha);
});
});
describe('#delay()', function() {
it('should set the delay option to true', function() {
var mocha = new Mocha(opts);
mocha.delay();
expect(mocha.options, 'to have property', 'delay', true);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.delay(), 'to be', mocha);
});
});
describe('#diff()', function() {
it('should set the diff option to true', function() {
var mocha = new Mocha(opts);
mocha.diff();
expect(mocha.options, 'to have property', 'diff', true);
});
it('should set the diff option to false', function() {
var mocha = new Mocha(opts);
mocha.diff(false);
expect(mocha.options, 'to have property', 'diff', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.diff(), 'to be', mocha);
});
});
describe('#dispose()', function() {
it('should dispose the root suite', function() {
var mocha = new Mocha(opts);
var disposeStub = sandbox.stub(mocha.suite, 'dispose');
mocha.dispose();
expect(disposeStub, 'was called once');
});
it('should dispose previous test runner', function() {
var mocha = new Mocha(opts);
var runStub = sandbox.stub(Mocha.Runner.prototype, 'run');
var disposeStub = sandbox.stub(Mocha.Runner.prototype, 'dispose');
mocha.run();
runStub.callArg(0);
mocha.dispose();
expect(disposeStub, 'was called once');
});
it('should unload the files', function() {
var mocha = new Mocha(opts);
var unloadFilesStub = sandbox.stub(mocha, 'unloadFiles');
mocha.dispose();
expect(unloadFilesStub, 'was called once');
});
});
describe('#forbidOnly()', function() {
it('should set the forbidOnly option to true', function() {
var mocha = new Mocha(opts);
mocha.forbidOnly();
expect(mocha.options, 'to have property', 'forbidOnly', true);
});
it('should set the forbidOnly option to false', function() {
var mocha = new Mocha(opts);
mocha.forbidOnly(false);
expect(mocha.options, 'to have property', 'forbidOnly', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.forbidOnly(), 'to be', mocha);
});
});
describe('#forbidPending()', function() {
it('should set the forbidPending option to true', function() {
var mocha = new Mocha(opts);
mocha.forbidPending();
expect(mocha.options, 'to have property', 'forbidPending', true);
});
it('should set the forbidPending option to false', function() {
var mocha = new Mocha(opts);
mocha.forbidPending(false);
expect(mocha.options, 'to have property', 'forbidPending', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.forbidPending(), 'to be', mocha);
});
});
describe('#fullTrace()', function() {
it('should set the fullTrace option to true', function() {
var mocha = new Mocha(opts);
mocha.fullTrace();
expect(mocha.options, 'to have property', 'fullTrace', true);
});
it('should set the fullTrace option to false', function() {
var mocha = new Mocha(opts);
mocha.fullTrace(false);
expect(mocha.options, 'to have property', 'fullTrace', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.fullTrace(), 'to be', mocha);
});
});
describe('#global()', function() {
it('should be an empty array initially', function() {
var mocha = new Mocha();
expect(mocha.options.global, 'to be empty');
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.global(), 'to be', mocha);
});
describe('when argument is invalid', function() {
it('should not modify the whitelist when given empty string', function() {
var mocha = new Mocha(opts);
mocha.global('');
expect(mocha.options.global, 'to be empty');
});
it('should not modify the whitelist when given empty array', function() {
var mocha = new Mocha(opts);
mocha.global([]);
expect(mocha.options.global, 'to be empty');
});
});
describe('when argument is valid', function() {
var elem = 'foo';
var elem2 = 'bar';
var elem3 = 'baz';
it('should add string to the whitelist', function() {
var mocha = new Mocha(opts);
mocha.global(elem);
expect(mocha.options.global, 'to contain', elem);
expect(mocha.options.global, 'to have length', 1);
});
it('should add contents of string array to the whitelist', function() {
var mocha = new Mocha(opts);
var elems = [elem, elem2];
mocha.global(elems);
expect(mocha.options.global, 'to contain', elem, elem2);
expect(mocha.options.global, 'to have length', elems.length);
});
it('should not have duplicates', function() {
var mocha = new Mocha({global: [elem, elem2]});
var elems = [elem, elem2, elem3];
mocha.global(elems);
expect(mocha.options.global, 'to contain', elem, elem2, elem3);
expect(mocha.options.global, 'to have length', elems.length);
});
});
});
describe('#growl()', function() {
describe('if capable of notifications', function() {
it('should set the growl option to true', function() {
var mocha = new Mocha(opts);
mocha.isGrowlCapable = function forceEnable() {
return true;
};
mocha.growl();
expect(mocha.options, 'to have property', 'growl', true);
});
});
describe('if not capable of notifications', function() {
it('should set the growl option to false', function() {
var mocha = new Mocha(opts);
mocha.isGrowlCapable = function forceDisable() {
return false;
};
mocha.growl();
expect(mocha.options, 'to have property', 'growl', false);
});
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.growl(), 'to be', mocha);
});
});
describe('#inlineDiffs()', function() {
it('should set the inlineDiffs option to true', function() {
var mocha = new Mocha(opts);
mocha.inlineDiffs();
expect(mocha.options, 'to have property', 'inlineDiffs', true);
});
it('should set the inlineDiffs option to false', function() {
var mocha = new Mocha(opts);
mocha.inlineDiffs(false);
expect(mocha.options, 'to have property', 'inlineDiffs', false);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.inlineDiffs(), 'to be', mocha);
});
});
describe('#invert()', function() {
it('should set the invert option to true', function() {
var mocha = new Mocha(opts);
mocha.invert();
expect(mocha.options, 'to have property', 'invert', true);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.invert(), 'to be', mocha);
});
});
describe('#noHighlighting()', function() {
// :NOTE: Browser-only option...
it('should set the noHighlighting option to true', function() {
var mocha = new Mocha(opts);
mocha.noHighlighting();
expect(mocha.options, 'to have property', 'noHighlighting', true);
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.noHighlighting(), 'to be', mocha);
});
});
describe('#reporter()', function() {
it('should throw reporter error if an invalid reporter is given', function() {
var updatedOpts = {reporter: 'invalidReporter', reporterOptions: {}};
var throwError = function() {
// eslint-disable-next-line no-new
new Mocha(updatedOpts);
};
expect(throwError, 'to throw', {
message: "invalid reporter 'invalidReporter'",
code: 'ERR_MOCHA_INVALID_REPORTER',
reporter: 'invalidReporter'
});
});
it('should be chainable', function() {
var mocha = new Mocha(opts);
expect(mocha.reporter(), 'to be', mocha);
});
it('should keep reporterOption on options', function() {
var mocha = new Mocha({
reporter: 'spec',
reporterOption: {
foo: 'bar'
}
});
expect(mocha.options.reporterOption, 'to have property', 'foo', 'bar');
// To support the legacy property name that can be used by reporters
expect(mocha.options.reporterOptions, 'to have property', 'foo', 'bar');
});
it('should support legacy reporterOptions', function() {
var mocha = new Mocha({
reporter: 'spec',
reporterOptions: {
foo: 'bar'
}
});
expect(mocha.options.reporterOption, 'to have property', 'foo', 'bar');
// To support the legacy property name that can be used by reporters
expect(mocha.options.reporterOptions, 'to have property', 'foo', 'bar');
});
});
describe('#run(fn)', function() {
it('should execute the callback when complete', function(done) {
var mocha = new Mocha(opts);
sandbox.stub(Mocha.Runner.prototype, 'run').callsArg(0);
mocha.run(done);
});
it('should not raise errors if callback was not provided', function() {
sandbox.stub(Mocha.Runner.prototype, 'run');
var mocha = new Mocha(opts);
expect(function() {
mocha.run();
}, 'not to throw');
});
it('should catch the `start` event if no tests are provided', function(done) {
var mocha = new Mocha(opts);
mocha.run().on('start', done);
});
it('should catch the `end` event if no tests are provided', function(done) {
var mocha = new Mocha(opts);
mocha.run().on('end', done);
});
it('should throw if a run is in progress', function() {
var mocha = new Mocha(opts);
var runStub = sandbox.stub(Mocha.Runner.prototype, 'run');
mocha.run();
expect(
function() {
mocha.run();
},
'to throw',
{
message:
'Mocha instance is currently running tests, cannot start a next test run until this one is done',
code: 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING',
instance: mocha
}
);
expect(runStub, 'was called once');
});
it('should throw the instance is already disposed', function() {
var mocha = new Mocha(opts);
var runStub = sandbox.stub(Mocha.Runner.prototype, 'run');
mocha.dispose();
expect(
function() {
mocha.run();
},
'to throw',
{
message:
'Mocha instance is already disposed, cannot start a new test run. Please create a new mocha instance. Be sure to set disable `cleanReferencesAfterRun` when you want to reuse the same mocha instance for multiple test runs.',
code: 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED',
cleanReferencesAfterRun: true,
instance: mocha
}
);
expect(runStub, 'was called times', 0);
});
it('should throw if a run for a second time', function() {
var mocha = new Mocha(opts);
var runStub = sandbox.stub(Mocha.Runner.prototype, 'run');
mocha.run();
runStub.callArg(0);
expect(
function() {
mocha.run();
},
'to throw',
{
message:
'Mocha instance is already disposed, cannot start a new test run. Please create a new mocha instance. Be sure to set disable `cleanReferencesAfterRun` when you want to reuse the same mocha instance for multiple test runs.',
code: 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED',
instance: mocha
}
);
expect(runStub, 'was called once');
});
it('should allow multiple runs if `cleanReferencesAfterRun` is disabled', function() {
var mocha = new Mocha(opts);
var runStub = sandbox.stub(Mocha.Runner.prototype, 'run');
mocha.cleanReferencesAfterRun(false);
mocha.run();
runStub.callArg(0);
mocha.run();
runStub.callArg(0);
expect(runStub, 'called times', 2);
});
it('should reset between runs', function() {
var mocha = new Mocha(opts);
var runStub = sandbox.stub(Mocha.Runner.prototype, 'run');
var resetStub = sandbox.stub(Mocha.Suite.prototype, 'reset');
mocha.cleanReferencesAfterRun(false);
mocha.run();
runStub.callArg(0);
mocha.run();
expect(resetStub, 'was called once');
});
it('should dispose the previous runner when the next run starts', function() {
var mocha = new Mocha(opts);
var runStub = sandbox.stub(Mocha.Runner.prototype, 'run');
var disposeStub = sandbox.stub(Mocha.Runner.prototype, 'dispose');
mocha.cleanReferencesAfterRun(false);
mocha.run();
runStub.callArg(0);
expect(disposeStub, 'was not called');
mocha.run();
expect(disposeStub, 'was called once');
});
describe('#reporter("xunit")#run(fn)', function() {
// :TBD: Why does specifying reporter differentiate this test from preceding one
it('should not raise errors if callback was not provided', function() {
var mocha = new Mocha();
expect(function() {
try {
mocha.reporter('xunit').run();
} catch (e) {
console.log(e);
expect.fail(e.message);
}
}, 'not to throw');
});
});
});
describe('#unloadFiles()', function() {
it('should reset referencesCleaned and allow for next run', function() {
var mocha = new Mocha(opts);
var runStub = sandbox.stub(Mocha.Runner.prototype, 'run');
mocha.run();
runStub.callArg(0);
mocha.unloadFiles();
expect(function() {
mocha.run();
}, 'not to throw');
});
it('should not be allowed when the current instance is already disposed', function() {
var mocha = new Mocha(opts);
mocha.dispose();
expect(
function() {
mocha.unloadFiles();
},
'to throw',
'Mocha instance is already disposed, it cannot be used again.'
);
});
});
});