diff --git a/lib/test.js b/lib/test.js index ca343b6f..9cd9492e 100644 --- a/lib/test.js +++ b/lib/test.js @@ -216,7 +216,7 @@ Test.prototype._assert = function assert (ok, opts) { if (!ok) { var e = new Error('exception'); var err = (e.stack || '').split('\n'); - var dir = path.dirname(__dirname) + path.sep; + var dir = __dirname + path.sep; for (var i = 0; i < err.length; i++) { var m = /^[^\s]*\s*\bat\s+(.+)/.exec(err[i]); diff --git a/test/circular-things.js b/test/circular-things.js index bbabc919..4bda3e14 100644 --- a/test/circular-things.js +++ b/test/circular-things.js @@ -20,6 +20,7 @@ tap.test('circular test', function (assert) { + ' {}\n' + ' actual: |-\n' + ' { circular: [Circular] }\n' + + ' at: Test. ($TEST/circular-things.js:$LINE:$COL)\n' + ' stack: |-\n' + ' Error: should be equal\n' + ' [... stack stripped ...]\n' diff --git a/test/common.js b/test/common.js index c8f7db54..4ded4e5f 100644 --- a/test/common.js +++ b/test/common.js @@ -1,3 +1,4 @@ +var path = require('path'); var yaml = require('js-yaml'); module.exports.getDiag = function (body) { @@ -7,10 +8,11 @@ module.exports.getDiag = function (body) { return line.slice(2); }).join('\n'); - // The stack trace will vary depending on where the code is run, so just - // strip it out. + // The stack trace and at variable will vary depending on where the code + // is run, so just strip it out. var withStack = yaml.safeLoad(diag); delete withStack.stack; + delete withStack.at; return withStack; } @@ -19,7 +21,8 @@ module.exports.getDiag = function (body) { // 1) The base checkout directory of tape might change. Because stack traces // include absolute paths, the stack traces will change depending on the // checkout path. We handle this by replacing the base test directory with a -// placeholder $TEST variable. +// placeholder $TEST variable and the package root with a placehodler +// $TAPE variable. // 2) Line positions within the file might change. We handle this by replacing // line and column markers with placeholder $LINE and $COL "variables" // a) node 0.8 does not provide nested eval line numbers, so we remove them @@ -34,8 +37,9 @@ module.exports.stripFullStack = function (output) { var m = line.match(/[ ]{8}at .*\((.*)\)/); var stripChangingData = function (line) { - var withoutDirectory = line.replace(__dirname, '$TEST'); - var withoutLineNumbers = withoutDirectory.replace(/:\d+:\d+/g, ':$LINE:$COL'); + var withoutTestDir = line.replace(__dirname, '$TEST'); + var withoutPackageDir = withoutTestDir.replace(path.dirname(__dirname), '$TAPE'); + var withoutLineNumbers = withoutPackageDir.replace(/:\d+:\d+/g, ':$LINE:$COL'); var withoutNestedLineNumbers = withoutLineNumbers.replace(/, \:\$LINE:\$COL\)$/, ')'); return withoutNestedLineNumbers; } diff --git a/test/deep-equal-failure.js b/test/deep-equal-failure.js index 4836037b..4c0d4eed 100644 --- a/test/deep-equal-failure.js +++ b/test/deep-equal-failure.js @@ -26,6 +26,7 @@ tap.test('deep equal failure', function (assert) { + ' { b: 2 }\n' + ' actual: |-\n' + ' { a: 1 }\n' + + ' at: Test. ($TEST/deep-equal-failure.js:$LINE:$COL)\n' + ' stack: |-\n' + ' Error: should be equal\n' + ' [... stack stripped ...]\n' @@ -48,6 +49,7 @@ tap.test('deep equal failure', function (assert) { parser.once('assert', function (data) { delete data.diag.stack; + delete data.diag.at; assert.deepEqual(data, { ok: false, id: 1, @@ -85,6 +87,7 @@ tap.test('deep equal failure, depth 6, with option', function (assert) { + ' { a: { a1: { a2: { a3: { a4: { a5: 2 } } } } } }\n' + ' actual: |-\n' + ' { a: { a1: { a2: { a3: { a4: { a5: 1 } } } } } }\n' + + ' at: Test. ($TEST/deep-equal-failure.js:$LINE:$COL)\n' + ' stack: |-\n' + ' Error: should be equal\n' + ' [... stack stripped ...]\n' @@ -107,6 +110,7 @@ tap.test('deep equal failure, depth 6, with option', function (assert) { parser.once('assert', function (data) { delete data.diag.stack; + delete data.diag.at; assert.deepEqual(data, { ok: false, id: 1, @@ -144,6 +148,7 @@ tap.test('deep equal failure, depth 6, without option', function (assert) { + ' { a: { a1: { a2: { a3: { a4: [Object] } } } } }\n' + ' actual: |-\n' + ' { a: { a1: { a2: { a3: { a4: [Object] } } } } }\n' + + ' at: Test. ($TEST/deep-equal-failure.js:$LINE:$COL)\n' + ' stack: |-\n' + ' Error: should be equal\n' + ' [... stack stripped ...]\n' @@ -166,6 +171,7 @@ tap.test('deep equal failure, depth 6, without option', function (assert) { parser.once('assert', function (data) { delete data.diag.stack; + delete data.diag.at; assert.deepEqual(data, { ok: false, id: 1, diff --git a/test/double_end.js b/test/double_end.js index 212606e9..5457cb27 100644 --- a/test/double_end.js +++ b/test/double_end.js @@ -24,6 +24,7 @@ test(function (t) { to._onTimeout = doEnd; var stackExpected; + var atExpected; try { to._onTimeout(); } @@ -31,6 +32,7 @@ test(function (t) { stackExpected = stripFullStack(e.stack).split('\n')[1]; stackExpected = stackExpected.replace('double_end.js', 'double_end/double.js'); stackExpected = stackExpected.trim(); + atExpected = stackExpected.replace(/^at\s+/, 'at: '); } var stripped = stripFullStack(body.toString('utf8')); @@ -41,6 +43,7 @@ test(function (t) { 'not ok 2 .end() called twice', ' ---', ' operator: fail', + ' ' + atExpected, ' stack: |-', ' Error: .end() called twice', ' [... stack stripped ...]', diff --git a/test/exit.js b/test/exit.js index 2cf9a9b5..2a9d2f0c 100644 --- a/test/exit.js +++ b/test/exit.js @@ -52,6 +52,7 @@ tap.test('exit fail', function (t) { ' operator: deepEqual', ' expected: [ [ 1, 2, [ 3, 4444 ] ], [ 5, 6 ] ]', ' actual: [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]', + ' at: Test. ($TEST/exit/fail.js:$LINE:$COL)', ' stack: |-', ' Error: should be equivalent', ' [... stack stripped ...]', @@ -93,6 +94,7 @@ tap.test('too few exit', function (t) { ' operator: fail', ' expected: 6', ' actual: 5', + ' at: process. ($TAPE/index.js:$LINE:$COL)', ' stack: |-', ' Error: plan != count', ' [... stack stripped ...]', @@ -127,6 +129,7 @@ tap.test('more planned in a second test', function (t) { ' operator: fail', ' expected: 2', ' actual: 1', + ' at: process. ($TAPE/index.js:$LINE:$COL)', ' stack: |-', ' Error: plan != count', ' [... stack stripped ...]', diff --git a/test/fail.js b/test/fail.js index 9aeb914b..73029639 100644 --- a/test/fail.js +++ b/test/fail.js @@ -22,6 +22,7 @@ tap.test('array test', function (tt) { ' operator: deepEqual', ' expected: [ [ 1, 2, [ 3, 4444 ] ], [ 5, 6 ] ]', ' actual: [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]', + ' at: Test. ($TEST/fail.js:$LINE:$COL)', ' stack: |-', ' Error: should be equivalent', ' [... stack stripped ...]', diff --git a/test/stackTrace.js b/test/stackTrace.js index 110da365..90cc39f6 100644 --- a/test/stackTrace.js +++ b/test/stackTrace.js @@ -13,6 +13,7 @@ tap.test('preserves stack trace with newlines', function (tt) { var stackTrace = 'foo\n bar'; parser.once('assert', function (data) { + delete data.diag.at; tt.deepEqual(data, { ok: false, id: 1, @@ -27,7 +28,8 @@ tap.test('preserves stack trace with newlines', function (tt) { }); stream.pipe(concat(function (body) { - var body = body.toString('utf8') + var body = body.toString('utf8'); + body = stripAt(body); tt.equal( body, 'TAP version 13\n' @@ -67,7 +69,7 @@ tap.test('preserves stack trace with newlines', function (tt) { }); tap.test('preserves stack trace for failed assertions', function (tt) { - tt.plan(5); + tt.plan(6); var test = tape.createHarness(); var stream = test.createStream(); @@ -75,14 +77,17 @@ tap.test('preserves stack trace for failed assertions', function (tt) { var stack = '' parser.once('assert', function (data) { - tt.equal(typeof data.diag.stack, 'string') - stack = data.diag.stack || '' + tt.equal(typeof data.diag.at, 'string'); + tt.equal(typeof data.diag.stack, 'string'); + at = data.diag.at || ''; + stack = data.diag.stack || ''; tt.ok(/^Error: false should be true(\n at .+)+/.exec(stack), 'stack should be a stack') tt.deepEqual(data, { ok: false, id: 1, name: "false should be true", diag: { + at: at, stack: stack, operator: 'equal', expected: false, @@ -92,7 +97,8 @@ tap.test('preserves stack trace for failed assertions', function (tt) { }); stream.pipe(concat(function (body) { - var body = body.toString('utf8') + var body = body.toString('utf8'); + body = stripAt(body); tt.equal( body, 'TAP version 13\n' @@ -134,5 +140,13 @@ function getDiag (body) { return line.slice(2); }).join('\n'); - return yaml.safeLoad(diag); + // Get rid of 'at' variable (which has a line number / path of its own that's + // difficult to check). + var withStack = yaml.safeLoad(diag); + delete withStack.at; + return withStack; } + +function stripAt (body) { + return body.replace(/^\s*at:\s+Test.*$\n/m, ''); +} \ No newline at end of file diff --git a/test/throws.js b/test/throws.js index 197ce68d..5b576eb1 100644 --- a/test/throws.js +++ b/test/throws.js @@ -40,6 +40,7 @@ tap.test('failures', function (tt) { + ' undefined\n' + ' actual: |-\n' + " { [TypeError: " + getNonFunctionMessage() + "] message: '" + getNonFunctionMessage() + "' }\n" + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' TypeError: ' + getNonFunctionMessage(undefined) + '\n' + ' [... stack stripped ...]\n' @@ -53,6 +54,7 @@ tap.test('failures', function (tt) { + ' undefined\n' + ' actual: |-\n' + " { [TypeError: " + getNonFunctionMessage(null) + "] message: '" + getNonFunctionMessage(null) + "' }\n" + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' TypeError: ' + getNonFunctionMessage(null) + '\n' + ' [... stack stripped ...]\n' @@ -66,6 +68,7 @@ tap.test('failures', function (tt) { + ' undefined\n' + ' actual: |-\n' + " { [TypeError: " + getNonFunctionMessage(true) + "] message: '" + getNonFunctionMessage(true) + "' }\n" + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' TypeError: ' + getNonFunctionMessage(true) + '\n' + ' [... stack stripped ...]\n' @@ -79,6 +82,7 @@ tap.test('failures', function (tt) { + ' undefined\n' + ' actual: |-\n' + " { [TypeError: " + getNonFunctionMessage(false) + "] message: '" + getNonFunctionMessage(false) + "' }\n" + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' TypeError: ' + getNonFunctionMessage(false) + '\n' + ' [... stack stripped ...]\n' @@ -92,6 +96,7 @@ tap.test('failures', function (tt) { + ' undefined\n' + ' actual: |-\n' + " { [TypeError: " + getNonFunctionMessage('abc') + "] message: '" + getNonFunctionMessage('abc') + "' }\n" + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' TypeError: ' + getNonFunctionMessage('abc') + '\n' + ' [... stack stripped ...]\n' @@ -105,6 +110,7 @@ tap.test('failures', function (tt) { + ' undefined\n' + ' actual: |-\n' + " { [TypeError: " + getNonFunctionMessage(/a/g) + "] message: '" + getNonFunctionMessage(/a/g) + "' }\n" + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' TypeError: ' + getNonFunctionMessage(/a/g) + '\n' + ' [... stack stripped ...]\n' @@ -118,6 +124,7 @@ tap.test('failures', function (tt) { + ' undefined\n' + ' actual: |-\n' + " { [TypeError: " + getNonFunctionMessage([]) + "] message: '" + getNonFunctionMessage([]) + "' }\n" + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' TypeError: ' + getNonFunctionMessage([]) + '\n' + ' [... stack stripped ...]\n' @@ -131,6 +138,7 @@ tap.test('failures', function (tt) { + ' undefined\n' + ' actual: |-\n' + " { [TypeError: " + getNonFunctionMessage({}) + "] message: '" + getNonFunctionMessage({}) + "' }\n" + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' TypeError: ' + getNonFunctionMessage({}) + '\n' + ' [... stack stripped ...]\n' @@ -143,6 +151,7 @@ tap.test('failures', function (tt) { + ' operator: throws\n' + ' expected: undefined\n' + ' actual: undefined\n' + + ' at: Test. ($TEST/throws.js:$LINE:$COL)\n' + ' stack: |-\n' + ' Error: should throw\n' + ' [... stack stripped ...]\n' diff --git a/test/too_many.js b/test/too_many.js index 04cea91f..b7d59e86 100644 --- a/test/too_many.js +++ b/test/too_many.js @@ -22,6 +22,7 @@ tap.test('array test', function (tt) { ' operator: fail', ' expected: 3', ' actual: 4', + ' at: Test. ($TEST/too_many.js:$LINE:$COL)', ' stack: |-', ' Error: plan != count', ' [... stack stripped ...]', diff --git a/test/undef.js b/test/undef.js index a3101a26..f809384b 100644 --- a/test/undef.js +++ b/test/undef.js @@ -20,6 +20,7 @@ tap.test('array test', function (tt) { + ' { beep: undefined }\n' + ' actual: |-\n' + ' {}\n' + + ' at: Test. ($TEST/undef.js:$LINE:$COL)\n' + ' stack: |-\n' + ' Error: should be equivalent\n' + ' [... stack stripped ...]\n'