/
runner.spec.js
610 lines (536 loc) · 18.7 KB
/
runner.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
'use strict';
var Mocha = require('../../lib/mocha');
var Suite = Mocha.Suite;
var Runner = Mocha.Runner;
var Test = Mocha.Test;
var Runnable = Mocha.Runnable;
var Hook = Mocha.Hook;
var path = require('path');
var noop = Mocha.utils.noop;
var EVENT_TEST_FAIL = Runner.constants.EVENT_TEST_FAIL;
var EVENT_TEST_RETRY = Runner.constants.EVENT_TEST_RETRY;
var EVENT_RUN_END = Runner.constants.EVENT_RUN_END;
var STATE_FAILED = Runnable.constants.STATE_FAILED;
describe('Runner', function() {
var suite;
var runner;
beforeEach(function() {
suite = new Suite('Suite', 'root');
runner = new Runner(suite);
});
describe('.grep()', function() {
it('should update the runner.total with number of matched tests', function() {
suite.addTest(new Test('im a test about lions', noop));
suite.addTest(new Test('im another test about lions', noop));
suite.addTest(new Test('im a test about bears', noop));
var newRunner = new Runner(suite);
newRunner.grep(/lions/);
expect(newRunner.total, 'to be', 2);
});
it('should update the runner.total with number of matched tests when inverted', function() {
suite.addTest(new Test('im a test about lions', noop));
suite.addTest(new Test('im another test about lions', noop));
suite.addTest(new Test('im a test about bears', noop));
var newRunner = new Runner(suite);
newRunner.grep(/lions/, true);
expect(newRunner.total, 'to be', 1);
});
});
describe('.grepTotal()', function() {
it('should return the total number of matched tests', function() {
suite.addTest(new Test('im a test about lions', noop));
suite.addTest(new Test('im another test about lions', noop));
suite.addTest(new Test('im a test about bears', noop));
runner.grep(/lions/);
expect(runner.grepTotal(suite), 'to be', 2);
});
it('should return the total number of matched tests when inverted', function() {
suite.addTest(new Test('im a test about lions', noop));
suite.addTest(new Test('im another test about lions', noop));
suite.addTest(new Test('im a test about bears', noop));
runner.grep(/lions/, true);
expect(runner.grepTotal(suite), 'to be', 1);
});
});
describe('.globalProps()', function() {
it('should include common non enumerable globals', function() {
var props = runner.globalProps();
expect(
props,
'to contain',
'setTimeout',
'clearTimeout',
'setInterval',
'clearInterval',
'Date',
'XMLHttpRequest'
);
});
});
describe('.globals()', function() {
it('should default to the known globals', function() {
expect(runner.globals().length, 'to be greater than', 16);
});
it('should white-list globals', function() {
runner.globals(['foo', 'bar']);
expect(runner.globals(), 'to contain', 'foo', 'bar');
});
});
describe('.checkGlobals(test)', function() {
before(function() {
if (!Object.create) {
this.skip();
}
});
it('should allow variables that match a wildcard', function(done) {
runner.globals(['foo*', 'giz*']);
global.foo = 'baz';
global.gizmo = 'quux';
runner.checkGlobals();
delete global.foo;
delete global.gizmo;
done();
});
it('should emit "fail" when a new global is introduced', function(done) {
var test = new Test('im a test', noop);
runner.checkGlobals();
global.foo = 'bar';
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_test, 'to be', test);
expect(_err, 'to have message', "global leak detected: 'foo'");
delete global.foo;
done();
});
runner.checkGlobals(test);
});
it('should emit "fail" when a single new disallowed global is introduced after a single extra global is allowed', function(done) {
var doneCalled = false;
runner.globals('good');
global.bad = 1;
runner.on(EVENT_TEST_FAIL, function() {
delete global.bad;
done();
doneCalled = true;
});
runner.checkGlobals(new Test('yet another test', noop));
if (!doneCalled) {
done(Error('Expected test failure did not occur.'));
}
});
it('should not fail when a new common global is introduced', function() {
if (process.browser) {
this.skip();
return;
}
// verify that the prop isn't enumerable
expect(global.propertyIsEnumerable('XMLHttpRequest'), 'to be', false);
// create a new runner and keep a reference to the test.
var test = new Test('im a test about bears', noop);
suite.addTest(test);
var newRunner = new Runner(suite);
// make the prop enumerable again.
global.XMLHttpRequest = noop;
expect(global.propertyIsEnumerable('XMLHttpRequest'), 'to be', true);
// verify the test hasn't failed.
newRunner.checkGlobals(test);
expect(test, 'not to have key', 'state');
// clean up our global space.
delete global.XMLHttpRequest;
});
it('should pluralize the error message when several are introduced', function(done) {
var test = new Test('im a test', noop);
runner.checkGlobals();
global.foo = 'bar';
global.bar = 'baz';
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_test, 'to be', test);
expect(_err, 'to have message', "global leaks detected: 'foo', 'bar'");
delete global.foo;
delete global.bar;
done();
});
runner.checkGlobals(test);
});
it('should respect per test whitelisted globals', function() {
var test = new Test('im a test about lions', noop);
test.globals(['foo']);
suite.addTest(test);
var runner = new Runner(suite);
global.foo = 'bar';
// verify the test hasn't failed.
runner.checkGlobals(test);
expect(test, 'not to have key', 'state');
delete global.foo;
});
it('should respect per test whitelisted globals but still detect other leaks', function(done) {
var test = new Test('im a test about lions', noop);
test.globals(['foo']);
suite.addTest(test);
global.foo = 'whitelisted';
global.bar = 'detect-me';
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_test.title, 'to be', 'im a test about lions');
expect(_err, 'to have message', "global leak detected: 'bar'");
delete global.foo;
delete global.bar;
done();
});
runner.checkGlobals(test);
});
it('should emit "fail" when a global beginning with "d" is introduced', function(done) {
global.derp = 'bar';
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_test.title, 'to be', 'herp');
expect(_err, 'to have message', "global leak detected: 'derp'");
delete global.derp;
done();
});
runner.checkGlobals(new Test('herp', noop));
});
});
describe('.hook(name, fn)', function() {
it('should execute hooks after failed test if suite bail is true', function(done) {
runner.fail(new Test('failed test', noop), new Error());
suite.bail(true);
suite.afterEach(function() {
suite.afterAll(function() {
done();
});
});
runner.hook('afterEach', noop);
runner.hook('afterAll', noop);
});
});
describe('.fail(test, err)', function() {
it('should increment .failures', function() {
expect(runner.failures, 'to be', 0);
runner.fail(new Test('one', noop), {});
expect(runner.failures, 'to be', 1);
runner.fail(new Test('two', noop), new Error());
expect(runner.failures, 'to be', 2);
});
it('should set test.state to "failed"', function() {
var test = new Test('some test', noop);
runner.fail(test, 'some error');
expect(test.state, 'to be', STATE_FAILED);
});
it('should emit "fail"', function(done) {
var test = new Test('some other test', noop);
var err = {};
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_test, 'to be', test);
expect(_err, 'to be an', Error);
expect(_err, 'not to be', {});
done();
});
runner.fail(test, err);
});
it('should emit a helpful message when failed with a string', function(done) {
var test = new Test('helpful test', noop);
var err = 'string';
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_err, 'to be an', Error);
expect(
_err,
'to have message',
'the string "string" was thrown, throw an Error :)'
);
done();
});
runner.fail(test, err);
});
it('should emit a the error when failed with an Error instance', function(done) {
var test = new Test('a test', noop);
var err = new Error('an error message');
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_err, 'to be an', Error);
expect(_err, 'to have message', 'an error message');
done();
});
runner.fail(test, err);
});
it('should emit the error when failed with an Error-like object', function(done) {
var test = new Test('a test', noop);
var err = {message: 'an error message'};
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_err, 'not to be an', Error);
expect(_err.message, 'to be', 'an error message');
done();
});
runner.fail(test, err);
});
it('should emit a helpful message when failed with an Object', function(done) {
var test = new Test('a test', noop);
var err = {x: 1};
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_err, 'to be an', Error);
expect(
_err,
'to have message',
'the object {\n "x": 1\n} was thrown, throw an Error :)'
);
done();
});
runner.fail(test, err);
});
it('should emit a helpful message when failed with an Array', function(done) {
var test = new Test('a test', noop);
var err = [1, 2];
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_err, 'to be an', Error);
expect(
_err,
'to have message',
'the array [\n 1\n 2\n] was thrown, throw an Error :)'
);
done();
});
runner.fail(test, err);
});
it('should recover if the error stack is not writable', function(done) {
if (!Object.create) {
this.skip();
return;
}
var err = new Error('not evil');
Object.defineProperty(err, 'stack', {
value: err.stack
});
var test = new Test('a test', noop);
runner.on(EVENT_TEST_FAIL, function(_test, _err) {
expect(_err, 'to have message', 'not evil');
done();
});
runner.fail(test, err);
});
it('should return and not increment failures when test is pending', function() {
var test = new Test('a test');
suite.addTest(test);
test.pending = true;
runner.fail(test, new Error());
expect(runner.failures, 'to be', 0);
});
});
describe('.failHook(hook, err)', function() {
it('should increment .failures', function() {
expect(runner.failures, 'to be', 0);
var test1 = new Test('fail hook 1', noop);
var test2 = new Test('fail hook 2', noop);
suite.addTest(test1);
suite.addTest(test2);
runner.failHook(test1, new Error('error1'));
expect(runner.failures, 'to be', 1);
runner.failHook(test2, new Error('error2'));
expect(runner.failures, 'to be', 2);
});
it('should augment hook title with current test title', function() {
var hook = new Hook('"before each" hook');
hook.ctx = {currentTest: new Test('should behave', noop)};
runner.failHook(hook, {});
expect(hook.title, 'to be', '"before each" hook for "should behave"');
hook.ctx.currentTest = new Test('should obey', noop);
runner.failHook(hook, {});
expect(hook.title, 'to be', '"before each" hook for "should obey"');
});
it('should emit "fail"', function(done) {
var hook = new Hook();
hook.parent = suite;
var err = new Error('error');
runner.on(EVENT_TEST_FAIL, function(_hook, _err) {
expect(_hook, 'to be', hook);
expect(_err, 'to be', err);
done();
});
runner.failHook(hook, err);
});
it('should not emit "end" if suite bail is not true', function(done) {
var hook = new Hook();
hook.parent = suite;
var err = new Error('error');
suite.bail(false);
runner.on(EVENT_RUN_END, function() {
throw new Error('"end" was emit, but the bail is false');
});
runner.failHook(hook, err);
done();
});
});
describe('.run(fn)', function() {
it('should emit "retry" when a retryable test fails', function(done) {
var retries = 2;
var retryableFails = 0;
var err = new Error('bear error');
var test = new Test('im a test about bears', function() {
if (retryableFails < retries) {
throw err;
}
});
suite.retries(retries);
suite.addTest(test);
runner.on(EVENT_TEST_RETRY, function(testClone, testErr) {
retryableFails += 1;
expect(testClone.title, 'to be', test.title);
expect(testErr, 'to be', err);
});
runner.run(function(failures) {
expect(failures, 'to be', 0);
expect(retryableFails, 'to be', retries);
done();
});
});
});
describe('allowUncaught', function() {
it('should allow unhandled errors to propagate through', function(done) {
var newRunner = new Runner(suite);
newRunner.allowUncaught = true;
newRunner.test = new Test('failing test', function() {
throw new Error('allow unhandled errors');
});
function fail() {
newRunner.runTest();
}
expect(fail, 'to throw', 'allow unhandled errors');
done();
});
});
describe('stackTrace', function() {
var stack = [
'AssertionError: foo bar',
'at EventEmitter.<anonymous> (/usr/local/dev/test.js:16:12)',
'at Context.<anonymous> (/usr/local/dev/test.js:19:5)',
'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)',
'Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)',
'/usr/local/lib/node_modules/mocha/lib/runner.js:452:12',
'next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)',
'/usr/local/lib/node_modules/mocha/lib/runner.js:309:7',
'next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)',
'Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)',
'at processImmediate [as _immediateCallback] (timers.js:321:17)'
];
before(function() {
// Only for Node running on Windows
if (process.platform === 'win32') {
var addDrive = function(str) {
var drive = 'C:';
var pos = str.indexOf(path.posix.sep);
return pos !== -1 ? str.slice(0, pos) + drive + str.slice(pos) : str;
};
var useWinPathSep = function(str) {
return str.split(path.posix.sep).join(path.win32.sep);
};
// Fake Windows pathnames in stacktrace
stack = stack.map(function(line) {
return useWinPathSep(addDrive(line));
});
}
});
describe('shortStackTrace', function() {
before(function() {
if (process.browser) {
this.skip();
}
});
it('should prettify the stack-trace', function(done) {
var hook = new Hook();
hook.parent = suite;
var err = new Error();
// Fake stack-trace
err.stack = stack.join('\n');
runner.on(EVENT_TEST_FAIL, function(_hook, _err) {
expect(_err.stack, 'to be', stack.slice(0, 3).join('\n'));
done();
});
runner.failHook(hook, err);
});
});
describe('longStackTrace', function() {
it('should display the full stack-trace', function(done) {
var hook = new Hook();
hook.parent = suite;
var err = new Error();
// Fake stack-trace
err.stack = stack.join('\n');
// Add --stack-trace option
runner.fullStackTrace = true;
runner.on(EVENT_TEST_FAIL, function(_hook, _err) {
expect(_err.stack, 'to be', stack.join('\n'));
done();
});
runner.failHook(hook, err);
});
});
describe('hugeStackTrace', function() {
before(function() {
if (process.browser) {
this.skip();
}
});
// Generate 64k string
function genOverlongSingleLineMessage() {
var n = 8200;
var data = [];
data.length = n;
for (var i = 0; i < n; i++) {
data[i] = {a: 1};
}
return JSON.stringify(data);
}
// Generate 64k string
function genOverlongMultiLineMessage() {
var n = 1150;
var data = [];
data.length = n;
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
for (var i = 0; i < n; i++) {
data[i] = str;
}
return data.join('\n');
}
it('should not hang if overlong error message is single line', function(done) {
var hook = new Hook();
hook.parent = suite;
var message = genOverlongSingleLineMessage();
var err = new Error();
// Fake stack-trace
err.stack = [message].concat(stack).join('\n');
runner.on(EVENT_TEST_FAIL, function(_hook, _err) {
var filteredErrStack = _err.stack.split('\n').slice(1);
expect(
filteredErrStack.join('\n'),
'to be',
stack.slice(0, 3).join('\n')
);
done();
});
runner.failHook(hook, err);
});
it('should not hang if overlong error message is multiple lines', function(done) {
var hook = new Hook();
hook.parent = suite;
var message = genOverlongMultiLineMessage();
var err = new Error();
// Fake stack-trace
err.stack = [message].concat(stack).join('\n');
runner.on(EVENT_TEST_FAIL, function(_hook, _err) {
var filteredErrStack = _err.stack.split('\n').slice(-3);
expect(
filteredErrStack.join('\n'),
'to be',
stack.slice(0, 3).join('\n')
);
done();
});
runner.failHook(hook, err);
});
});
});
describe('abort', function() {
it('should set _abort property to true', function() {
runner.abort();
expect(runner._abort, 'to be true');
});
it('should return the Runner', function() {
expect(runner.abort(), 'to be', runner);
});
});
});