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

assert: fix throws trace #18595

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 15 additions & 19 deletions lib/assert.js
Expand Up @@ -344,11 +344,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 compareKey(actual, expected, key, msg) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could we give this function a slightly more descriptive name since it only works for throws? Like compareExceptionKey or something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (!isDeepStrictEqual(actual[key], expected[key])) {
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 @@ -363,23 +369,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));
compareKey(actual, expected, 'name', msg);
}
if ('message' in expected) {
assert.strictEqual(
actual.message,
expected.message,
createMsg(msg, 'message', actual, expected));
compareKey(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)) {
compareKey(actual, expected, key, msg);
}
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions test/message/assert_throws_stack.js
@@ -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
@@ -0,0 +1,14 @@
assert.js:*
throw new 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 *