☕️ Classic, reliable, trusted test framework for Node.js and the browser
ensure windows compat with stack trace filter; closes #2502 (#2503)
Christopher Hiller8bf1f297142b
diff --git a/lib/utils.js b/lib/utils.jsindex 1cfbbae7..bb2c99c4 100644--- a/lib/utils.js+++ b/lib/utils.js@@ -9,7 +9,8 @@ var basename = require('path').basename;var debug = require('debug')('mocha:watch');var exists = require('fs').existsSync || require('path').existsSync;var glob = require('glob');-var join = require('path').join;+var path = require('path');+var join = path.join;var readdirSync = require('fs').readdirSync;var statSync = require('fs').statSync;var watchFile = require('fs').watchFile;@@ -720,16 +721,20 @@ exports.getError = function(err) {*/exports.stackTraceFilter = function() {// TODO: Replace with `process.browser`- var slash = '/';var is = typeof document === 'undefined' ? { node: true } : { browser: true };- var cwd = is.node- ? process.cwd() + slash- : (typeof location === 'undefined' ? window.location : location).href.replace(/\/[^\/]*$/, '/');+ var slash = path.sep;+ var cwd;+ if (is.node) {+ cwd = process.cwd() + slash;+ } else {+ cwd = (typeof location === 'undefined' ? window.location : location).href.replace(/\/[^\/]*$/, '/');+ slash = '/';+ }function isMochaInternal(line) {return (~line.indexOf('node_modules' + slash + 'mocha' + slash))- || (~line.indexOf('components' + slash + 'mochajs' + slash))- || (~line.indexOf('components' + slash + 'mocha' + slash))+ || (~line.indexOf('node_modules' + slash + 'mocha.js'))+ || (~line.indexOf('bower_components' + slash + 'mocha.js'))|| (~line.indexOf(slash + 'mocha.js'));}diff --git a/test/runner.spec.js b/test/runner.spec.jsindex ffe4d70e..5392d007 100644--- a/test/runner.spec.js+++ b/test/runner.spec.js@@ -3,6 +3,7 @@ var Suite = mocha.Suite;var Runner = mocha.Runner;var Test = mocha.Test;var Hook = mocha.Hook;+var path = require('path');function noop() {}@@ -373,6 +374,12 @@ describe('Runner', function() {];describe('shortStackTrace', function() {+ beforeEach(function() {+ if (path.sep !== '/') {+ this.skip();+ }+ });+it('should prettify the stack-trace', function(done) {var hook = new Hook();var err = new Error();@@ -388,6 +395,12 @@ describe('Runner', function() {});describe('longStackTrace', function() {+ beforeEach(function() {+ if (path.sep !== '/') {+ this.skip();+ }+ });+it('should display the full stack-trace', function(done) {var hook = new Hook();var err = new Error();diff --git a/test/utils.spec.js b/test/utils.spec.jsindex 501ea7ab..4683809a 100644--- a/test/utils.spec.js+++ b/test/utils.spec.js@@ -1,5 +1,6 @@var mocha = require('..');var utils = mocha.utils;+var path = require('path');var JSON = require('json3');describe('utils', function() {@@ -97,62 +98,120 @@ describe('utils', function() {describe('.stackTraceFilter()', function() {describe('on node', function() {var filter = utils.stackTraceFilter();- it('should get a stack-trace as a string and prettify it', 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)'];- filter(stack.join('\n')).should.equal(stack.slice(0,3).join('\n'));-- stack = [ 'AssertionError: bar baz'- , 'at /usr/local/dev/some-test-file.js:25:8'- , 'at tryCatcher (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/util.js:24:31)'- , 'at Promise._resolveFromResolver (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:439:31)'- , 'at new Promise (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:53:37)'- , 'at yourFunction (/usr/local/dev/own/tmp/test1.js:24:13)'- , 'at Context.<anonymous> (/usr/local/dev/some-test-file:30:4)'- , 'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)'- , '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)'];- filter(stack.join('\n')).should.equal(stack.slice(0,7).join('\n'));- });+ describe('on POSIX OS', function () {+ before(function () {+ if (path.sep !== '/') {+ this.skip();+ }+ });- it('does not ignore other bower_components and components', function() {- var stack = ['Error: failed'- , 'at assert (index.html:11:26)'- , 'at Context.<anonymous> (test.js:17:18)'- , 'at bower_components/should/should.js:4827:7'- , 'at next (file:///.../bower_components/should/should.js:4766:23)'- , 'at components/should/5.0.0/should.js:4827:7'- , 'at next (file:///.../components/should/5.0.0/should.js:4766:23)'- , 'at file:///.../bower_components/mocha/mocha.js:4794:5'- , 'at timeslice (.../components/mocha/mocha.js:6218:27)'- , 'at Test.require.register.Runnable.run (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4463:15)'- , 'at Runner.require.register.Runner.runTest (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4892:10)'- , 'at file:///.../components/mochajs/mocha/2.1.0/mocha.js:4970:12'- , 'at next (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4817:14)'];- filter(stack.join('\n')).should.equal(stack.slice(0,7).join('\n'));- });+ it('should get a stack-trace as a string and prettify it', 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)'+ ];+ filter(stack.join('\n'))+ .should+ .equal(stack.slice(0, 3)+ .join('\n'));++ stack = [+ 'AssertionError: bar baz',+ 'at /usr/local/dev/some-test-file.js:25:8',+ 'at tryCatcher (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/util.js:24:31)',+ 'at Promise._resolveFromResolver (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:439:31)',+ 'at new Promise (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:53:37)',+ 'at yourFunction (/usr/local/dev/own/tmp/test1.js:24:13)',+ 'at Context.<anonymous> (/usr/local/dev/some-test-file:30:4)',+ 'Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:218:15)',+ '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)'+ ];++ filter(stack.join('\n'))+ .should+ .equal(stack.slice(0, 7)+ .join('\n'));+ });++ it('does not ignore other bower_components and components',+ function () {+ var stack = [+ 'Error: failed',+ 'at assert (index.html:11:26)',+ 'at Context.<anonymous> (test.js:17:18)',+ 'at bower_components/should/should.js:4827:7',+ 'at next (file:///.../bower_components/should/should.js:4766:23)',+ 'at components/should/5.0.0/should.js:4827:7',+ 'at next (file:///.../components/should/5.0.0/should.js:4766:23)',+ 'at file:///.../bower_components/mocha/mocha.js:4794:5',+ 'at timeslice (.../components/mocha/mocha.js:6218:27)',+ 'at Test.require.register.Runnable.run (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4463:15)',+ 'at Runner.require.register.Runner.runTest (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4892:10)',+ 'at file:///.../components/mochajs/mocha/2.1.0/mocha.js:4970:12',+ 'at next (file:///.../components/mochajs/mocha/2.1.0/mocha.js:4817:14)'+ ];+ filter(stack.join('\n'))+ .should+ .equal(stack.slice(0, 7)+ .join('\n'));+ });- it('should replace absolute with relative paths', function() {- var stack = ['Error: ' + process.cwd() + '/bla.js has a problem'- , 'at foo (' + process.cwd() + '/foo/index.js:13:226)'- , 'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'];+ it('should replace absolute with relative paths', function () {+ var stack = [+ 'Error: ' + process.cwd() + '/bla.js has a problem',+ 'at foo (' + process.cwd() + '/foo/index.js:13:226)',+ 'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'+ ];++ var expected = [+ 'Error: ' + process.cwd() + '/bla.js has a problem',+ 'at foo (foo/index.js:13:226)',+ 'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'+ ];++ filter(stack.join('\n'))+ .should+ .equal(expected.join('\n'));+ });+ });- var expected = ['Error: ' + process.cwd() + '/bla.js has a problem'- , 'at foo (foo/index.js:13:226)'- , 'at bar (/usr/local/dev/own/tmp/node_modules/bluebird/js/main/promise.js:11:26)'];+ describe('on Windows', function() {+ before(function () {+ if (path.sep === '/') {+ this.skip();+ }+ });- filter(stack.join('\n')).should.equal(expected.join('\n'));+ it('should work on Windows', function () {+ var stack = [+ 'Error: failed',+ 'at Context.<anonymous> (C:\\Users\\ishida\\src\\test\\test\\mytest.js:5:9)',+ 'at callFn (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runnable.js:326:21)',+ 'at Test.Runnable.run (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runnable.js:319:7)',+ 'at Runner.runTest (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:422:10)',+ 'at C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:528:12',+ 'at next (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:342:14)',+ 'at C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:352:7',+ 'at next (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:284:14)',+ 'at Immediate._onImmediate (C:\\Users\\ishida\\src\\test\\node_modules\\mocha\\lib\\runner.js:320:5)'+ ];+ filter(stack.join('\n'))+ .should+ .equal(stack.slice(0, 2)+ .join('\n'));+ });});});@@ -163,7 +222,7 @@ describe('utils', function() {global.location = { href: 'localhost:3000/foo/bar/index.html' };filter = utils.stackTraceFilter();});- it('does not strip out other bower_components and components', function() {+ it('does not strip out other bower_components', function() {var stack = ['Error: failed', 'at assert (index.html:11:26)', 'at Context.<anonymous> (test.js:17:18)'