Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RegExp for matching stack frames to handle Promise/then scenario (fixes #515). #516

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ Test.prototype._assert = function assert(ok, opts) {
for (var i = 0; i < err.length; i++) {
/*
Stack trace lines may resemble one of the following. We need
to should correctly extract a function name (if any) and
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
path / line no. for each line.
to correctly extract a function name (if any) and path / line
number for each line.

at myFunction (/path/to/file.js:123:45)
at myFunction (/path/to/file.other-ext:123:45)
Expand Down Expand Up @@ -299,9 +299,9 @@ Test.prototype._assert = function assert(ok, opts) {
Last part captures file path plus line no (and optional
column no).

/((?:\/|[a-zA-Z]:\\)[^:\)]+:(\d+)(?::(\d+))?)/
/((?:\/|[a-zA-Z]:\\)[^:\)]+:(\d+)(?::(\d+))?)\)?/
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
*/
var re = /^(?:[^\s]*\s*\bat\s+)(?:(.*)\s+\()?((?:\/|[a-zA-Z]:\\)[^:\)]+:(\d+)(?::(\d+))?)\)$/;
var re = /^(?:[^\s]*\s*\bat\s+)(?:(.*)\s+\()?((?:\/|[a-zA-Z]:\\)[^:\)]+:(\d+)(?::(\d+))?)\)?$/;
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
var lineWithTokens = err[i].replace(process.cwd(), '/\$CWD').replace(__dirname, '/\$TEST');
var m = re.exec(lineWithTokens);

Expand Down
2 changes: 1 addition & 1 deletion test/anonymous-fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tap.test('inside anonymous functions', function (tt) {
'not ok 1 fail',
' ---',
' operator: fail',
' at: Test.<anonymous> ($TEST/anonymous-fn/test-wrapper.js:$LINE:$COL)',
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
' at: <anonymous> ($TEST/anonymous-fn.js:$LINE:$COL)',
' stack: |-',
' Error: fail',
' [... stack stripped ...]',
Expand Down
2 changes: 1 addition & 1 deletion test/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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.<anonymous> ($TEST/exit/fail.js:$LINE:$COL)',
' at: <anonymous> ($TEST/exit/fail.js:$LINE:$COL)',
' stack: |-',
' Error: should be deeply equivalent',
' [... stack stripped ...]',
Expand Down
2 changes: 1 addition & 1 deletion test/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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.<anonymous> ($TEST/fail.js:$LINE:$COL)',
' at: <anonymous> ($TEST/fail.js:$LINE:$COL)',
' stack: |-',
' Error: should be deeply equivalent',
' [... stack stripped ...]',
Expand Down
68 changes: 64 additions & 4 deletions test/stackTrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ tap.test('preserves stack trace with newlines', function (tt) {
});
});

tap.test('parses function name from original stack', function (tt) {
tt.plan(1);
tap.test('parses function info from original stack', function (tt) {
tt.plan(4);
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved

var test = tape.createHarness();
test.createStream();

test._results._watch = function (t) {
t.on('result', function (res) {
tt.equal('Test.testFunctionNameParsing', res.functionName);
tt.match(res.file, /stackTrace.js/i);
tt.ok(Number(res.line) > 0);
tt.ok(Number(res.column) > 0);
});
};

Expand All @@ -80,15 +83,18 @@ tap.test('parses function name from original stack', function (tt) {
});
});

tap.test('parses function name from original stack for anonymous function', function (tt) {
tt.plan(1);
tap.test('parses function info from original stack for anonymous function', function (tt) {
tt.plan(4);

var test = tape.createHarness();
test.createStream();

test._results._watch = function (t) {
t.on('result', function (res) {
tt.equal('Test.<anonymous>', res.functionName);
tt.match(res.file, /stackTrace.js/i);
tt.ok(Number(res.line) > 0);
tt.ok(Number(res.column) > 0);
});
};

Expand All @@ -98,6 +104,60 @@ tap.test('parses function name from original stack for anonymous function', func
});
});

if (typeof Promise === 'function' && typeof Promise.resolve === 'function') {

tap.test('parses function info from original stack for Promise scenario', function (tt) {
tt.plan(4);

var test = tape.createHarness();
test.createStream();

test._results._watch = function (t) {
t.on('result', function (res) {
tt.equal('onfulfilled', res.functionName);
tt.match(res.file, /stackTrace.js/i);
tt.ok(Number(res.line) > 0);
tt.ok(Number(res.column) > 0);
});
};

test('t.equal stack trace', function testFunctionNameParsing(t) {
new Promise(function (resolve) {
resolve();
}).then(function onfulfilled() {
t.equal(true, false, 'true should be false');
t.end();
});
});
});

tap.test('parses function info from original stack for Promise scenario with anonymous function', function (tt) {
tt.plan(4);

var test = tape.createHarness();
test.createStream();

test._results._watch = function (t) {
t.on('result', function (res) {
tt.equal('<anonymous>', res.functionName);
tt.match(res.file, /stackTrace.js/i);
tt.ok(Number(res.line) > 0);
tt.ok(Number(res.column) > 0);
});
};

test('t.equal stack trace', function testFunctionNameParsing(t) {
new Promise(function (resolve) {
resolve();
}).then(function () {
t.equal(true, false, 'true should be false');
t.end();
});
});
});

}

tap.test('preserves stack trace for failed assertions', function (tt) {
tt.plan(6);

Expand Down
2 changes: 1 addition & 1 deletion test/too_many.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tap.test('array test', function (tt) {
' operator: fail',
' expected: 3',
' actual: 4',
' at: Test.<anonymous> ($TEST/too_many.js:$LINE:$COL)',
' at: <anonymous> ($TEST/too_many.js:$LINE:$COL)',
' stack: |-',
' Error: plan != count',
' [... stack stripped ...]',
Expand Down