Skip to content

Commit

Permalink
assert: fix throws trace
Browse files Browse the repository at this point in the history
The current stack trace thrown in case `assert.throws(fn, object)`
is used did not filter the stack trace. This fixes it.

Backport-PR-URL: #23223
PR-URL: #18595
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
BridgeAR authored and MylesBorins committed Oct 31, 2018
1 parent 562787e commit 18071db
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
34 changes: 15 additions & 19 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,17 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
}
};

function createMsg(msg, key, actual, expected) {
if (msg)
return msg;
return `${key}: expected ${inspect(expected[key])}, ` +
`not ${inspect(actual[key])}`;
function compareExceptionKey(actual, expected, key, msg) {
if (!innerDeepEqual(actual[key], expected[key], true)) {
innerFail({
actual: actual[key],
expected: expected[key],
message: msg || `${key}: expected ${inspect(expected[key])}, ` +
`not ${inspect(actual[key])}`,
operator: 'throws',
stackStartFn: assert.throws
});
}
}

function expectedException(actual, expected, msg) {
Expand All @@ -680,23 +686,13 @@ function expectedException(actual, expected, msg) {
// The name and message could be non enumerable. Therefore test them
// explicitly.
if ('name' in expected) {
assert.strictEqual(
actual.name,
expected.name,
createMsg(msg, 'name', actual, expected));
compareExceptionKey(actual, expected, 'name', msg);
}
if ('message' in expected) {
assert.strictEqual(
actual.message,
expected.message,
createMsg(msg, 'message', actual, expected));
compareExceptionKey(actual, expected, 'message', msg);
}
const keys = Object.keys(expected);
for (const key of keys) {
assert.deepStrictEqual(
actual[key],
expected[key],
createMsg(msg, key, actual, expected));
for (const key of Object.keys(expected)) {
compareExceptionKey(actual, expected, key, msg);
}
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions test/message/assert_throws_stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

require('../common');
const assert = require('assert').strict;

assert.throws(() => { throw new Error('foo'); }, { bar: true });
14 changes: 14 additions & 0 deletions test/message/assert_throws_stack.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
assert.js:*
throw new errors.AssertionError(obj);
^

AssertionError [ERR_ASSERTION]: bar: expected true, not undefined
at Object.<anonymous> (*assert_throws_stack.js:*:*)
at *
at *
at *
at *
at *
at *
at *
at *

0 comments on commit 18071db

Please sign in to comment.