Skip to content

Commit d0cc3b2

Browse files
authored
test(unit/runner.spec.js): Minor tweeks related to #3686 (#3704)
* Refactored portions of tests for increased clarity. * Updated code to run all stacktrace tests on Windows (previously skipped). * Updated/replaced logic for generation of overlong messages used in filter tests.
1 parent a406503 commit d0cc3b2

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

test/unit/runner.spec.js

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22

3+
var path = require('path');
34
var mocha = require('../../lib/mocha');
45
var Suite = mocha.Suite;
56
var Runner = mocha.Runner;
67
var Test = mocha.Test;
78
var Hook = mocha.Hook;
8-
var path = require('path');
9-
var fs = require('fs');
109
var noop = mocha.utils.noop;
1110

1211
describe('Runner', function() {
@@ -452,9 +451,29 @@ describe('Runner', function() {
452451
'at processImmediate [as _immediateCallback] (timers.js:321:17)'
453452
];
454453

454+
before(function() {
455+
// Only for Node running on Windows
456+
if (process.platform === 'win32') {
457+
var addDrive = function(str) {
458+
var drive = 'C:';
459+
var pos = str.indexOf(path.posix.sep);
460+
return pos !== -1 ? str.slice(0, pos) + drive + str.slice(pos) : str;
461+
};
462+
463+
var useWinPathSep = function(str) {
464+
return str.split(path.posix.sep).join(path.win32.sep);
465+
};
466+
467+
// Fake Windows pathnames in stacktrace
468+
stack = stack.map(function(line) {
469+
return useWinPathSep(addDrive(line));
470+
});
471+
}
472+
});
473+
455474
describe('shortStackTrace', function() {
456-
beforeEach(function() {
457-
if (path.sep !== '/') {
475+
before(function() {
476+
if (process.browser) {
458477
this.skip();
459478
}
460479
});
@@ -475,12 +494,6 @@ describe('Runner', function() {
475494
});
476495

477496
describe('longStackTrace', function() {
478-
beforeEach(function() {
479-
if (path.sep !== '/') {
480-
this.skip();
481-
}
482-
});
483-
484497
it('should display the full stack-trace', function(done) {
485498
var hook = new Hook();
486499
hook.parent = suite;
@@ -499,29 +512,47 @@ describe('Runner', function() {
499512
});
500513

501514
describe('hugeStackTrace', function() {
502-
beforeEach(function() {
503-
if (path.sep !== '/') {
515+
before(function() {
516+
if (process.browser) {
504517
this.skip();
505518
}
506519
});
507520

508-
it('should not hang if the error message is ridiculously long single line', function(done) {
521+
// Generate 64k string
522+
function genOverlongSingleLineMessage() {
523+
var n = 8200;
524+
var data = [];
525+
data.length = n;
526+
for (var i = 0; i < n; i++) {
527+
data[i] = {a: 1};
528+
}
529+
return JSON.stringify(data);
530+
}
531+
532+
// Generate 64k string
533+
function genOverlongMultiLineMessage() {
534+
var n = 1150;
535+
var data = [];
536+
data.length = n;
537+
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
538+
for (var i = 0; i < n; i++) {
539+
data[i] = str;
540+
}
541+
return data.join('\n');
542+
}
543+
544+
it('should not hang if overlong error message is single line', function(done) {
509545
var hook = new Hook();
510546
hook.parent = suite;
511-
var data = [];
512-
// mock a long message
513-
for (var i = 0; i < 10000; i++) data[i] = {a: 1};
514-
var message = JSON.stringify(data);
547+
var message = genOverlongSingleLineMessage();
515548
var err = new Error();
516549
// Fake stack-trace
517550
err.stack = [message].concat(stack).join('\n');
518551

519552
runner.on('fail', function(hook, err) {
553+
var filteredErrStack = err.stack.split('\n').slice(1);
520554
expect(
521-
err.stack
522-
.split('\n')
523-
.slice(1)
524-
.join('\n'),
555+
filteredErrStack.join('\n'),
525556
'to be',
526557
stack.slice(0, 3).join('\n')
527558
);
@@ -530,21 +561,18 @@ describe('Runner', function() {
530561
runner.failHook(hook, err);
531562
});
532563

533-
it('should not hang if error message is ridiculously long multiple lines either', function(done) {
564+
it('should not hang if overlong error message is multiple lines', function(done) {
534565
var hook = new Hook();
535566
hook.parent = suite;
536-
var fpath = path.join(__dirname, '../../mocha.js');
537-
var message = fs.readFileSync(fpath, 'utf8');
567+
var message = genOverlongMultiLineMessage();
538568
var err = new Error();
539569
// Fake stack-trace
540570
err.stack = [message].concat(stack).join('\n');
541571

542572
runner.on('fail', function(hook, err) {
573+
var filteredErrStack = err.stack.split('\n').slice(-3);
543574
expect(
544-
err.stack
545-
.split('\n')
546-
.slice(-3)
547-
.join('\n'),
575+
filteredErrStack.join('\n'),
548576
'to be',
549577
stack.slice(0, 3).join('\n')
550578
);

0 commit comments

Comments
 (0)