forked from mochajs/mocha
/
suite.spec.js
685 lines (574 loc) · 20.6 KB
/
suite.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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
'use strict';
const Mocha = require('../../lib/mocha');
const {Suite, Test, Context} = Mocha;
const sinon = require('sinon');
const errors = require('../../lib/errors');
function supportsFunctionNames() {
// eslint-disable-next-line no-extra-parens
return function foo() {}.name === 'foo';
}
describe('Suite', function() {
afterEach(function() {
sinon.restore();
});
describe('instance method', function() {
let suite;
describe('clone()', function() {
beforeEach(function() {
suite = new Suite('To be cloned', {}, true);
suite._timeout = 3043;
suite._slow = 101;
suite._bail = true;
suite.suites.push(1);
suite.tests.push('hello');
suite._beforeEach.push(2);
suite._beforeAll.push(3);
suite._afterEach.push(4);
suite._afterAll.push(5);
});
it('should clone the Suite, omitting children', function() {
expect(suite.clone(), 'to satisfy', {
title: 'To be cloned',
_timeout: 3043,
_slow: 101,
_bail: true,
suites: expect.it('to be empty'),
tests: expect.it('to be empty'),
_beforeEach: expect.it('to be empty'),
_beforeAll: expect.it('to be empty'),
_afterEach: expect.it('to be empty'),
_afterAll: expect.it('to be empty'),
root: true
}).and('not to be', suite);
});
});
describe('reset()', function() {
beforeEach(function() {
suite = new Suite('Suite to be reset', function() {});
});
it('should reset the `delayed` state', function() {
suite.delayed = true;
suite.reset();
expect(suite.delayed, 'to be', false);
});
it('should forward reset to suites and tests', function() {
const childSuite = new Suite('child suite', suite.context);
const test = new Test('test', function() {});
suite.addSuite(childSuite);
suite.addTest(test);
const testResetStub = sinon.stub(test, 'reset');
const suiteResetStub = sinon.stub(childSuite, 'reset');
suite.reset();
expect(testResetStub, 'was called once');
expect(suiteResetStub, 'was called once');
});
it('should forward reset to all hooks', function() {
suite.beforeEach(function() {});
suite.afterEach(function() {});
suite.beforeAll(function() {});
suite.afterAll(function() {});
sinon.stub(suite.getHooks('beforeEach')[0], 'reset');
sinon.stub(suite.getHooks('afterEach')[0], 'reset');
sinon.stub(suite.getHooks('beforeAll')[0], 'reset');
sinon.stub(suite.getHooks('afterAll')[0], 'reset');
suite.reset();
expect(suite.getHooks('beforeEach')[0].reset, 'was called once');
expect(suite.getHooks('afterEach')[0].reset, 'was called once');
expect(suite.getHooks('beforeAll')[0].reset, 'was called once');
expect(suite.getHooks('afterAll')[0].reset, 'was called once');
});
});
describe('timeout()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('when no argument is passed', function() {
it('should return the timeout value', function() {
expect(suite.timeout(), 'to be', 2000);
});
});
describe('when argument is passed', function() {
it('should return the Suite object', function() {
const newSuite = suite.timeout(5000);
expect(newSuite.timeout(), 'to be', 5000);
});
});
});
describe('slow()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('when given a string', function() {
it('should parse it', function() {
suite.slow('5 seconds');
expect(suite.slow(), 'to be', 5000);
});
});
describe('when no argument is passed', function() {
it('should return the slow value', function() {
expect(suite.slow(), 'to be', 75);
});
});
describe('when argument is passed', function() {
it('should return the Suite object', function() {
const newSuite = suite.slow(5000);
expect(newSuite.slow(), 'to be', 5000);
});
});
});
describe('bail()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
suite._bail = true;
});
describe('when no argument is passed', function() {
it('should return the bail value', function() {
expect(suite.bail(), 'to be', true);
});
});
describe('when argument is passed', function() {
it('should return the Suite object', function() {
const newSuite = suite.bail(false);
expect(newSuite.bail(), 'to be', false);
});
});
});
describe('beforeAll()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('wraps the passed in function in a Hook', function() {
it('adds it to _beforeAll', function() {
const fn = function() {};
suite.beforeAll(fn);
expect(suite._beforeAll, 'to have length', 1);
const beforeAllItem = suite._beforeAll[0];
expect(beforeAllItem.title, 'to match', /^"before all" hook/);
expect(beforeAllItem.fn, 'to be', fn);
});
it('appends title to hook', function() {
const fn = function() {};
suite.beforeAll('test', fn);
expect(suite._beforeAll, 'to have length', 1);
const beforeAllItem = suite._beforeAll[0];
expect(beforeAllItem.title, 'to be', '"before all" hook: test');
expect(beforeAllItem.fn, 'to be', fn);
});
it('uses function name if available', function() {
if (!supportsFunctionNames()) {
this.skip();
return;
}
function namedFn() {}
suite.beforeAll(namedFn);
const beforeAllItem = suite._beforeAll[0];
expect(beforeAllItem.title, 'to be', '"before all" hook: namedFn');
expect(beforeAllItem.fn, 'to be', namedFn);
});
});
});
describe('afterAll()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('wraps the passed in function in a Hook', function() {
it('adds it to _afterAll', function() {
const fn = function() {};
suite.afterAll(fn);
expect(suite._afterAll, 'to have length', 1);
const afterAllItem = suite._afterAll[0];
expect(afterAllItem.title, 'to match', /^"after all" hook/);
expect(afterAllItem.fn, 'to be', fn);
});
it('appends title to hook', function() {
const fn = function() {};
suite.afterAll('test', fn);
expect(suite._afterAll, 'to have length', 1);
const beforeAllItem = suite._afterAll[0];
expect(beforeAllItem.title, 'to be', '"after all" hook: test');
expect(beforeAllItem.fn, 'to be', fn);
});
it('uses function name if available', function() {
if (!supportsFunctionNames()) {
this.skip();
return;
}
function namedFn() {}
suite.afterAll(namedFn);
const afterAllItem = suite._afterAll[0];
expect(afterAllItem.title, 'to be', '"after all" hook: namedFn');
expect(afterAllItem.fn, 'to be', namedFn);
});
});
});
describe('beforeEach()', function() {
let suite;
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('wraps the passed in function in a Hook', function() {
it('adds it to _beforeEach', function() {
const fn = function() {};
suite.beforeEach(fn);
expect(suite._beforeEach, 'to have length', 1);
const beforeEachItem = suite._beforeEach[0];
expect(beforeEachItem.title, 'to match', /^"before each" hook/);
expect(beforeEachItem.fn, 'to be', fn);
});
it('appends title to hook', function() {
const fn = function() {};
suite.beforeEach('test', fn);
expect(suite._beforeEach, 'to have length', 1);
const beforeAllItem = suite._beforeEach[0];
expect(beforeAllItem.title, 'to be', '"before each" hook: test');
expect(beforeAllItem.fn, 'to be', fn);
});
it('uses function name if available', function() {
if (!supportsFunctionNames()) {
this.skip();
return;
}
function namedFn() {}
suite.beforeEach(namedFn);
const beforeEachItem = suite._beforeEach[0];
expect(beforeEachItem.title, 'to be', '"before each" hook: namedFn');
expect(beforeEachItem.fn, 'to be', namedFn);
});
});
describe('when the suite is pending', function() {
beforeEach(function() {
suite.pending = true;
});
it('should not create a hook', function() {
suite.beforeEach(function() {});
expect(suite._beforeEach, 'to be empty');
});
});
});
describe('afterEach()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('wraps the passed in function in a Hook', function() {
it('adds it to _afterEach', function() {
const fn = function() {};
suite.afterEach(fn);
expect(suite._afterEach, 'to have length', 1);
const afterEachItem = suite._afterEach[0];
expect(afterEachItem.title, 'to match', /^"after each" hook/);
expect(afterEachItem.fn, 'to be', fn);
});
it('appends title to hook', function() {
const fn = function() {};
suite.afterEach('test', fn);
expect(suite._afterEach, 'to have length', 1);
const beforeAllItem = suite._afterEach[0];
expect(beforeAllItem.title, 'to be', '"after each" hook: test');
expect(beforeAllItem.fn, 'to be', fn);
});
it('uses function name if available', function() {
if (!supportsFunctionNames()) {
this.skip();
return;
}
function namedFn() {}
suite.afterEach(namedFn);
const afterEachItem = suite._afterEach[0];
expect(afterEachItem.title, 'to be', '"after each" hook: namedFn');
expect(afterEachItem.fn, 'to be', namedFn);
});
});
});
describe('create()', function() {
let first;
let second;
before(function() {
first = new Suite('Root suite', {}, true);
second = new Suite('RottenRoot suite', {}, true);
first.addSuite(second);
});
it('does not create a second root suite', function() {
expect(second.parent, 'to be', first);
expect(first.root, 'to be', true);
expect(second.root, 'to be', false);
});
it('does not denote the root suite by being titleless', function() {
const emptyTitleSuite = Suite.create(second, '');
expect(emptyTitleSuite.parent, 'to be', second);
expect(emptyTitleSuite.root, 'to be', false);
expect(second.root, 'to be', false);
});
});
describe('addSuite()', function() {
let first;
let second;
beforeEach(function() {
first = new Suite('First suite');
first.timeout(4002);
first.slow(200);
second = new Suite('Second suite');
first.addSuite(second);
});
it('sets the parent on the added Suite', function() {
expect(second.parent, 'to be', first);
});
it('copies the timeout value', function() {
expect(second.timeout(), 'to be', 4002);
});
it('copies the slow value', function() {
expect(second.slow(), 'to be', 200);
});
it('adds the suite to the suites collection', function() {
expect(first.suites, 'to have length', 1);
expect(first.suites[0], 'to be', second);
});
it('treats suite as pending if its parent is pending', function() {
first.pending = true;
expect(second.isPending(), 'to be', true);
});
});
describe('addTest()', function() {
let test;
beforeEach(function() {
suite = new Suite('A Suite', new Context());
suite.timeout(4002);
test = new Test('test');
suite.addTest(test);
});
it('sets the parent on the added test', function() {
expect(test.parent, 'to be', suite);
});
it('copies the timeout value', function() {
expect(test.timeout(), 'to be', 4002);
});
it('adds the test to the tests collection', function() {
expect(suite.tests, 'to satisfy', [test]).and('to have length', 1);
});
});
describe('fullTitle()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('when there is no parent', function() {
it('returns the suite title', function() {
expect(suite.fullTitle(), 'to be', 'A Suite');
});
});
describe('when there is a parent', function() {
it("returns the combination of parent's and suite's title", function() {
const parentSuite = new Suite('I am a parent');
parentSuite.addSuite(suite);
expect(suite.fullTitle(), 'to be', 'I am a parent A Suite');
});
});
});
describe('titlePath()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('when there is no parent', function() {
it('returns the suite title', function() {
expect(suite.titlePath(), 'to equal', ['A Suite']);
});
});
describe('when there is a parent', function() {
describe('the parent is the root suite', function() {
it('returns the suite title', function() {
const rootSuite = new Suite('', {}, true);
rootSuite.addSuite(suite);
expect(suite.titlePath(), 'to equal', ['A Suite']);
});
});
describe('the parent is not the root suite', function() {
it("returns the concatenation of parent's and suite's title", function() {
const parentSuite = new Suite('I am a parent');
parentSuite.addSuite(suite);
expect(suite.titlePath(), 'to equal', ['I am a parent', 'A Suite']);
});
});
});
});
describe('total()', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('when there are no nested suites or tests', function() {
it('should return 0', function() {
expect(suite.total(), 'to be', 0);
});
});
describe('when there are several tests in the suite', function() {
it('should return the number', function() {
suite.addTest(new Test('a child test'));
suite.addTest(new Test('another child test'));
expect(suite.total(), 'to be', 2);
});
});
});
describe('eachTest(fn)', function() {
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('when there are no nested suites or tests', function() {
it('should return 0', function() {
let n = 0;
function fn() {
n++;
}
suite.eachTest(fn);
expect(n, 'to be', 0);
});
});
describe('when there are several tests in the suite', function() {
it('should return the number', function() {
suite.addTest(new Test('a child test'));
suite.addTest(new Test('another child test'));
let n = 0;
function fn() {
n++;
}
suite.eachTest(fn);
expect(n, 'to be', 2);
});
});
describe('when there are several levels of nested suites', function() {
it('should return the number', function() {
suite.addTest(new Test('a child test'));
const childSuite = new Suite('a child suite');
childSuite.addTest(new Test('a test in a child suite'));
suite.addSuite(childSuite);
let n = 0;
function fn() {
n++;
}
suite.eachTest(fn);
expect(n, 'to be', 2);
});
});
});
describe('constructor', function() {
beforeEach(function() {
sinon.stub(errors, 'deprecate');
});
/* eslint no-new: off */
it("should throw an error if the title isn't a string", function() {
expect(function() {
new Suite(undefined, 'root');
}, 'to throw');
expect(function() {
new Suite(function() {}, 'root');
}, 'to throw');
});
it('should not throw if the title is a string', function() {
expect(function() {
new Suite('Bdd suite', 'root');
}, 'not to throw');
});
});
describe('timeout()', function() {
it('should convert a string to milliseconds', function() {
const suite = new Suite('some suite');
suite.timeout('100');
expect(suite.timeout(), 'to be', 100);
});
});
describe('hasOnly()', function() {
it('should return true if a test has `only`', function() {
const suite = new Suite('foo');
const test = new Test('bar');
suite.appendOnlyTest(test);
expect(suite.hasOnly(), 'to be', true);
});
it('should return true if a suite has `only`', function() {
const suite = new Suite('foo');
const nested = new Suite('bar');
suite.appendOnlySuite(nested);
expect(suite.hasOnly(), 'to be', true);
});
it('should return true if nested suite has `only`', function() {
const suite = new Suite('foo');
const nested = new Suite('bar');
const test = new Test('baz');
nested.appendOnlyTest(test);
// `nested` has a `only` test, but `suite` doesn't know about it
suite.suites.push(nested);
expect(suite.hasOnly(), 'to be', true);
});
it('should return false if no suite or test is marked `only`', function() {
const suite = new Suite('foo');
const nested = new Suite('bar');
const test = new Test('baz');
suite.suites.push(nested);
nested.tests.push(test);
expect(suite.hasOnly(), 'to be', false);
});
});
describe('filterOnly()', function() {
it('should filter out all other tests and suites if a test has `only`', function() {
const suite = new Suite('a');
const nested = new Suite('b');
const test = new Test('c');
const test2 = new Test('d');
suite.suites.push(nested);
suite.appendOnlyTest(test);
suite.tests.push(test2);
suite.filterOnly();
expect(suite, 'to satisfy', {
suites: expect.it('to be empty'),
tests: expect
.it('to have length', 1)
.and('to have an item satisfying', {title: 'c'})
});
});
it('should filter out all other tests and suites if a suite has `only`', function() {
const suite = new Suite('a');
const nested1 = new Suite('b');
const nested2 = new Suite('c');
const test = new Test('d');
const nestedTest = new Test('e');
nested1.appendOnlyTest(nestedTest);
suite.tests.push(test);
suite.suites.push(nested1);
suite.appendOnlySuite(nested1);
suite.suites.push(nested2);
suite.filterOnly();
expect(suite, 'to satisfy', {
suites: expect
.it('to have length', 1)
.and('to have an item satisfying', {title: 'b'}),
tests: expect.it('to be empty')
});
});
});
describe('markOnly()', function() {
it('should call appendOnlySuite on parent', function() {
const suite = new Suite('foo');
const spy = sinon.spy();
suite.parent = {
appendOnlySuite: spy
};
suite.markOnly();
expect(spy, 'to have a call exhaustively satisfying', [suite]).and(
'was called once'
);
});
});
});
});
describe('Test', function() {
describe('initialization', function() {
it("should throw an error if the title isn't a string", function() {
expect(function() {
new Test(function() {});
}, 'to throw');
expect(() => {
new Test(undefined, function() {});
}, 'to throw');
});
it('should not throw if the title is a string', function() {
expect(function() {
new Test('test-case', function() {});
}, 'not to throw');
});
});
});