From e69f34868343ca197e5b39bbe322f80e52e82ba1 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Wed, 30 Jan 2019 12:50:44 -0600 Subject: [PATCH 1/5] test(unit/runner.spec.js): Minor tweeks related to #3686 Refactored portions of tests for (hopefully) increased clarity. --- test/unit/runner.spec.js | 47 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index df5bd210c1..e8b1d2d110 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -1,12 +1,12 @@ 'use strict'; +var fs = require('fs'); +var path = require('path'); var mocha = require('../../lib/mocha'); var Suite = mocha.Suite; var Runner = mocha.Runner; var Test = mocha.Test; var Hook = mocha.Hook; -var path = require('path'); -var fs = require('fs'); var noop = mocha.utils.noop; describe('Runner', function() { @@ -454,7 +454,7 @@ describe('Runner', function() { describe('shortStackTrace', function() { beforeEach(function() { - if (path.sep !== '/') { + if (process.platform === 'win32') { this.skip(); } }); @@ -476,7 +476,7 @@ describe('Runner', function() { describe('longStackTrace', function() { beforeEach(function() { - if (path.sep !== '/') { + if (process.platform === 'win32') { this.skip(); } }); @@ -500,28 +500,31 @@ describe('Runner', function() { describe('hugeStackTrace', function() { beforeEach(function() { - if (path.sep !== '/') { + if (process.platform === 'win32') { this.skip(); } }); - it('should not hang if the error message is ridiculously long single line', function(done) { + function genOverlongSingleLineMessage() { + var data = []; + for (var i = 0; i < 10000; i++) { + data[i] = {a: 1}; + } + return JSON.stringify(data); + } + + it('should not hang if the overlong error message is single line', function(done) { var hook = new Hook(); hook.parent = suite; - var data = []; - // mock a long message - for (var i = 0; i < 10000; i++) data[i] = {a: 1}; - var message = JSON.stringify(data); + var message = genOverlongSingleLineMessage(); var err = new Error(); // Fake stack-trace err.stack = [message].concat(stack).join('\n'); runner.on('fail', function(hook, err) { + var filteredErrStack = err.stack.split('\n').slice(1); expect( - err.stack - .split('\n') - .slice(1) - .join('\n'), + filteredErrStack.join('\n'), 'to be', stack.slice(0, 3).join('\n') ); @@ -530,21 +533,23 @@ describe('Runner', function() { runner.failHook(hook, err); }); - it('should not hang if error message is ridiculously long multiple lines either', function(done) { + function genOverlongMultiLineMessage() { + var fpath = path.join(__dirname, '..', '..', 'mocha.js'); + return fs.readFileSync(fpath, 'utf8'); + } + + it('should not hang if overlong error message is multiple lines', function(done) { var hook = new Hook(); hook.parent = suite; - var fpath = path.join(__dirname, '../../mocha.js'); - var message = fs.readFileSync(fpath, 'utf8'); + var message = genOverlongMultiLineMessage(); var err = new Error(); // Fake stack-trace err.stack = [message].concat(stack).join('\n'); runner.on('fail', function(hook, err) { + var filteredErrStack = err.stack.split('\n').slice(-3); expect( - err.stack - .split('\n') - .slice(-3) - .join('\n'), + filteredErrStack.join('\n'), 'to be', stack.slice(0, 3).join('\n') ); From f920930242255ad5adf89f122a0309b08e738f34 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Wed, 30 Jan 2019 13:11:39 -0600 Subject: [PATCH 2/5] test(unit/runner.spec.js): Make wording consistent between two tests --- test/unit/runner.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index e8b1d2d110..7ba6efccc9 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -513,7 +513,7 @@ describe('Runner', function() { return JSON.stringify(data); } - it('should not hang if the overlong error message is single line', function(done) { + it('should not hang if overlong error message is single line', function(done) { var hook = new Hook(); hook.parent = suite; var message = genOverlongSingleLineMessage(); From 2bb00115afc79d468a4e4a2240bf337d2edd1623 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Thu, 31 Jan 2019 14:26:27 -0600 Subject: [PATCH 3/5] test(unit/runner.spec.js): Fixes and upgrades Converted code to (hopefully) run all stacktrace tests on Windows as well. Replaced logic for overlong multiline message. --- test/unit/runner.spec.js | 61 ++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index 7ba6efccc9..ab6d85d0fd 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -1,6 +1,5 @@ 'use strict'; -var fs = require('fs'); var path = require('path'); var mocha = require('../../lib/mocha'); var Suite = mocha.Suite; @@ -452,13 +451,27 @@ describe('Runner', function() { 'at processImmediate [as _immediateCallback] (timers.js:321:17)' ]; - describe('shortStackTrace', function() { - beforeEach(function() { - if (process.platform === 'win32') { - this.skip(); - } - }); + before(function() { + // Only for Node running on Windows + if ('platform' in process && 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() { it('should prettify the stack-trace', function(done) { var hook = new Hook(); hook.parent = suite; @@ -475,12 +488,6 @@ describe('Runner', function() { }); describe('longStackTrace', function() { - beforeEach(function() { - if (process.platform === 'win32') { - this.skip(); - } - }); - it('should display the full stack-trace', function(done) { var hook = new Hook(); hook.parent = suite; @@ -499,20 +506,29 @@ describe('Runner', function() { }); describe('hugeStackTrace', function() { - beforeEach(function() { - if (process.platform === 'win32') { - this.skip(); - } - }); - + // Generate 64k string function genOverlongSingleLineMessage() { + var n = 8200; var data = []; - for (var i = 0; i < 10000; i++) { + 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; @@ -533,11 +549,6 @@ describe('Runner', function() { runner.failHook(hook, err); }); - function genOverlongMultiLineMessage() { - var fpath = path.join(__dirname, '..', '..', 'mocha.js'); - return fs.readFileSync(fpath, 'utf8'); - } - it('should not hang if overlong error message is multiple lines', function(done) { var hook = new Hook(); hook.parent = suite; From 6f125b10f140df2778e106752c82e63c1e8ca14e Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Thu, 31 Jan 2019 16:42:38 -0600 Subject: [PATCH 4/5] test(unit/runner.spec.js): Skip stacktrace filter tests in browser --- test/unit/runner.spec.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index ab6d85d0fd..8216e363c5 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -472,6 +472,12 @@ describe('Runner', function() { }); describe('shortStackTrace', function() { + before(function() { + if ('browser' in process) { + this.skip(); + } + }); + it('should prettify the stack-trace', function(done) { var hook = new Hook(); hook.parent = suite; @@ -506,6 +512,12 @@ describe('Runner', function() { }); describe('hugeStackTrace', function() { + before(function() { + if ('browser' in process) { + this.skip(); + } + }); + // Generate 64k string function genOverlongSingleLineMessage() { var n = 8200; From 9e36ddc620afa1b03d0e940f6092600943937043 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Fri, 1 Feb 2019 07:42:58 -0600 Subject: [PATCH 5/5] test(unit/runner.spec.js): Skip existential operator checks --- test/unit/runner.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index 8216e363c5..0908e95e1e 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -453,7 +453,7 @@ describe('Runner', function() { before(function() { // Only for Node running on Windows - if ('platform' in process && process.platform === 'win32') { + if (process.platform === 'win32') { var addDrive = function(str) { var drive = 'C:'; var pos = str.indexOf(path.posix.sep); @@ -473,7 +473,7 @@ describe('Runner', function() { describe('shortStackTrace', function() { before(function() { - if ('browser' in process) { + if (process.browser) { this.skip(); } }); @@ -513,7 +513,7 @@ describe('Runner', function() { describe('hugeStackTrace', function() { before(function() { - if ('browser' in process) { + if (process.browser) { this.skip(); } });