-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
suite.spec.js
546 lines (464 loc) · 16.2 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
'use strict';
var mocha = require('../../lib/mocha');
var Suite = mocha.Suite;
var Test = mocha.Test;
function supportsFunctionNames() {
// eslint-disable-next-line no-extra-parens
return function foo() {}.name === 'foo';
}
describe('Suite', function() {
describe('.clone()', function() {
beforeEach(function() {
this.suite = new Suite('To be cloned');
this.suite._timeout = 3043;
this.suite._slow = 101;
this.suite._bail = true;
this.suite.suites.push(1);
this.suite.tests.push('hello');
this.suite._beforeEach.push(2);
this.suite._beforeAll.push(3);
this.suite._afterEach.push(4);
this.suite._afterAll.push(5);
});
it('should copy the title', function() {
expect(this.suite.clone().title, 'to be', 'To be cloned');
});
it('should copy the timeout value', function() {
expect(this.suite.clone().timeout(), 'to be', 3043);
});
it('should copy the slow value', function() {
expect(this.suite.clone().slow(), 'to be', 101);
});
it('should copy the bail value', function() {
expect(this.suite.clone().bail(), 'to be', true);
});
it('should not copy the values from the suites array', function() {
expect(this.suite.clone().suites, 'to be empty');
});
it('should not copy the values from the tests array', function() {
expect(this.suite.clone().tests, 'to be empty');
});
it('should not copy the values from the _beforeEach array', function() {
expect(this.suite.clone()._beforeEach, 'to be empty');
});
it('should not copy the values from the _beforeAll array', function() {
expect(this.suite.clone()._beforeAll, 'to be empty');
});
it('should not copy the values from the _afterEach array', function() {
expect(this.suite.clone()._afterEach, 'to be empty');
});
it('should not copy the values from the _afterAll array', function() {
expect(this.suite.clone()._afterAll, 'to be empty');
});
});
describe('.timeout()', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
});
describe('when no argument is passed', function() {
it('should return the timeout value', function() {
expect(this.suite.timeout(), 'to be', 2000);
});
});
describe('when argument is passed', function() {
it('should return the Suite object', function() {
var newSuite = this.suite.timeout(5000);
expect(newSuite.timeout(), 'to be', 5000);
});
});
});
describe('.slow()', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
});
describe('when given a string', function() {
it('should parse it', function() {
this.suite.slow('5 seconds');
expect(this.suite.slow(), 'to be', 5000);
});
});
describe('when no argument is passed', function() {
it('should return the slow value', function() {
expect(this.suite.slow(), 'to be', 75);
});
});
describe('when argument is passed', function() {
it('should return the Suite object', function() {
var newSuite = this.suite.slow(5000);
expect(newSuite.slow(), 'to be', 5000);
});
});
});
describe('.bail()', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
this.suite._bail = true;
});
describe('when no argument is passed', function() {
it('should return the bail value', function() {
expect(this.suite.bail(), 'to be', true);
});
});
describe('when argument is passed', function() {
it('should return the Suite object', function() {
var newSuite = this.suite.bail(false);
expect(newSuite.bail(), 'to be', false);
});
});
});
describe('.beforeAll()', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
});
describe('wraps the passed in function in a Hook', function() {
it('adds it to _beforeAll', function() {
var fn = function() {};
this.suite.beforeAll(fn);
expect(this.suite._beforeAll, 'to have length', 1);
var beforeAllItem = this.suite._beforeAll[0];
expect(beforeAllItem.title, 'to match', /^"before all" hook/);
expect(beforeAllItem.fn, 'to be', fn);
});
it('appends title to hook', function() {
var fn = function() {};
this.suite.beforeAll('test', fn);
expect(this.suite._beforeAll, 'to have length', 1);
var beforeAllItem = this.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() {}
this.suite.beforeAll(namedFn);
var beforeAllItem = this.suite._beforeAll[0];
expect(beforeAllItem.title, 'to be', '"before all" hook: namedFn');
expect(beforeAllItem.fn, 'to be', namedFn);
});
});
});
describe('.afterAll()', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
});
describe('wraps the passed in function in a Hook', function() {
it('adds it to _afterAll', function() {
var fn = function() {};
this.suite.afterAll(fn);
expect(this.suite._afterAll, 'to have length', 1);
var afterAllItem = this.suite._afterAll[0];
expect(afterAllItem.title, 'to match', /^"after all" hook/);
expect(afterAllItem.fn, 'to be', fn);
});
it('appends title to hook', function() {
var fn = function() {};
this.suite.afterAll('test', fn);
expect(this.suite._afterAll, 'to have length', 1);
var beforeAllItem = this.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() {}
this.suite.afterAll(namedFn);
var afterAllItem = this.suite._afterAll[0];
expect(afterAllItem.title, 'to be', '"after all" hook: namedFn');
expect(afterAllItem.fn, 'to be', namedFn);
});
});
});
describe('.beforeEach()', function() {
var suite;
beforeEach(function() {
suite = new Suite('A Suite');
});
describe('wraps the passed in function in a Hook', function() {
it('adds it to _beforeEach', function() {
var fn = function() {};
suite.beforeEach(fn);
expect(suite._beforeEach, 'to have length', 1);
var beforeEachItem = suite._beforeEach[0];
expect(beforeEachItem.title, 'to match', /^"before each" hook/);
expect(beforeEachItem.fn, 'to be', fn);
});
it('appends title to hook', function() {
var fn = function() {};
suite.beforeEach('test', fn);
expect(suite._beforeEach, 'to have length', 1);
var 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);
var 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() {
this.suite = new Suite('A Suite');
});
describe('wraps the passed in function in a Hook', function() {
it('adds it to _afterEach', function() {
var fn = function() {};
this.suite.afterEach(fn);
expect(this.suite._afterEach, 'to have length', 1);
var afterEachItem = this.suite._afterEach[0];
expect(afterEachItem.title, 'to match', /^"after each" hook/);
expect(afterEachItem.fn, 'to be', fn);
});
it('appends title to hook', function() {
var fn = function() {};
this.suite.afterEach('test', fn);
expect(this.suite._afterEach, 'to have length', 1);
var beforeAllItem = this.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() {}
this.suite.afterEach(namedFn);
var afterEachItem = this.suite._afterEach[0];
expect(afterEachItem.title, 'to be', '"after each" hook: namedFn');
expect(afterEachItem.fn, 'to be', namedFn);
});
});
});
describe('.create()', function() {
before(function() {
this.first = new Suite('Root suite', {}, true);
this.second = new Suite('RottenRoot suite', {}, true);
this.first.addSuite(this.second);
});
it('sets second root property to false', function() {
expect(this.second.parent, 'to be', this.first);
expect(this.first.root, 'to be', true);
expect(this.second.root, 'to be', false);
});
it('only one root property is set', function() {
var emptyTitleSuite = Suite.create(this.second, '');
expect(emptyTitleSuite.parent, 'to be', this.second);
expect(emptyTitleSuite.root, 'to be', false);
expect(this.second.root, 'to be', false);
});
});
describe('.addSuite()', function() {
beforeEach(function() {
this.first = new Suite('First suite');
this.first.timeout(4002);
this.first.slow(200);
this.second = new Suite('Second suite');
this.first.addSuite(this.second);
});
it('sets the parent on the added Suite', function() {
expect(this.second.parent, 'to be', this.first);
});
it('copies the timeout value', function() {
expect(this.second.timeout(), 'to be', 4002);
});
it('copies the slow value', function() {
expect(this.second.slow(), 'to be', 200);
});
it('adds the suite to the suites collection', function() {
expect(this.first.suites, 'to have length', 1);
expect(this.first.suites[0], 'to be', this.second);
});
it('treats suite as pending if its parent is pending', function() {
this.first.pending = true;
expect(this.second.isPending(), 'to be', true);
});
});
// describe('.addTest()', function(){
// beforeEach(function(){
// this.suite = new Suite('A Suite', new Context);
// this.suite.timeout(4002);
// this.test = new Test('test');
// this.suite.addTest(this.test);
// });
//
// it('sets the parent on the added test', function(){
// expect(this.test.parent, 'to be', this.suite);
// });
//
// it('copies the timeout value', function(){
// expect(this.test.timeout(), 'to be', 4002);
// });
//
// it('adds the test to the tests collection', function(){
// expect(this.suite.tests, 'to have length', 1);
// expect(this.suite.tests[0], 'to be', this.test);
// });
// });
describe('.fullTitle()', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
});
describe('when there is no parent', function() {
it('returns the suite title', function() {
expect(this.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() {
var parentSuite = new Suite('I am a parent');
parentSuite.addSuite(this.suite);
expect(this.suite.fullTitle(), 'to be', 'I am a parent A Suite');
});
});
});
describe('.titlePath()', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
});
describe('when there is no parent', function() {
it('returns the suite title', function() {
expect(this.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() {
var rootSuite = new Suite('', {}, true);
rootSuite.addSuite(this.suite);
expect(this.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() {
var parentSuite = new Suite('I am a parent');
parentSuite.addSuite(this.suite);
expect(this.suite.titlePath(), 'to equal', [
'I am a parent',
'A Suite'
]);
});
});
});
});
describe('.total()', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
});
describe('when there are no nested suites or tests', function() {
it('should return 0', function() {
expect(this.suite.total(), 'to be', 0);
});
});
describe('when there are several tests in the suite', function() {
it('should return the number', function() {
this.suite.addTest(new Test('a child test'));
this.suite.addTest(new Test('another child test'));
expect(this.suite.total(), 'to be', 2);
});
});
});
describe('.eachTest(fn)', function() {
beforeEach(function() {
this.suite = new Suite('A Suite');
});
describe('when there are no nested suites or tests', function() {
it('should return 0', function() {
var n = 0;
function fn() {
n++;
}
this.suite.eachTest(fn);
expect(n, 'to be', 0);
});
});
describe('when there are several tests in the suite', function() {
it('should return the number', function() {
this.suite.addTest(new Test('a child test'));
this.suite.addTest(new Test('another child test'));
var n = 0;
function fn() {
n++;
}
this.suite.eachTest(fn);
expect(n, 'to be', 2);
});
});
describe('when there are several levels of nested suites', function() {
it('should return the number', function() {
this.suite.addTest(new Test('a child test'));
var suite = new Suite('a child suite');
suite.addTest(new Test('a test in a child suite'));
this.suite.addSuite(suite);
var n = 0;
function fn() {
n++;
}
this.suite.eachTest(fn);
expect(n, 'to be', 2);
});
});
});
describe('initialization', function() {
/* 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() {
var suite = new Suite('some suite');
suite.timeout('100');
expect(suite.timeout(), 'to be', 100);
});
});
});
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(function() {
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');
});
});
});