assert: fix throws trace
The current stack trace thrown in case `assert.throws(fn, object)` is used did not filter the stack trace. This fixes it. PR-URL: https://github.com/nodejs/node/pull/18595 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
This commit is contained in:
parent
14bc3e22f3
commit
cccddc59e5
@ -361,11 +361,17 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function createMsg(msg, key, actual, expected) {
|
function compareExceptionKey(actual, expected, key, msg) {
|
||||||
if (msg)
|
if (!isDeepStrictEqual(actual[key], expected[key])) {
|
||||||
return msg;
|
innerFail({
|
||||||
return `${key}: expected ${inspect(expected[key])}, ` +
|
actual: actual[key],
|
||||||
`not ${inspect(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) {
|
function expectedException(actual, expected, msg) {
|
||||||
@ -380,23 +386,13 @@ function expectedException(actual, expected, msg) {
|
|||||||
// The name and message could be non enumerable. Therefore test them
|
// The name and message could be non enumerable. Therefore test them
|
||||||
// explicitly.
|
// explicitly.
|
||||||
if ('name' in expected) {
|
if ('name' in expected) {
|
||||||
assert.strictEqual(
|
compareExceptionKey(actual, expected, 'name', msg);
|
||||||
actual.name,
|
|
||||||
expected.name,
|
|
||||||
createMsg(msg, 'name', actual, expected));
|
|
||||||
}
|
}
|
||||||
if ('message' in expected) {
|
if ('message' in expected) {
|
||||||
assert.strictEqual(
|
compareExceptionKey(actual, expected, 'message', msg);
|
||||||
actual.message,
|
|
||||||
expected.message,
|
|
||||||
createMsg(msg, 'message', actual, expected));
|
|
||||||
}
|
}
|
||||||
const keys = Object.keys(expected);
|
for (const key of Object.keys(expected)) {
|
||||||
for (const key of keys) {
|
compareExceptionKey(actual, expected, key, msg);
|
||||||
assert.deepStrictEqual(
|
|
||||||
actual[key],
|
|
||||||
expected[key],
|
|
||||||
createMsg(msg, key, actual, expected));
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
6
test/message/assert_throws_stack.js
Normal file
6
test/message/assert_throws_stack.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
require('../common');
|
||||||
|
const assert = require('assert').strict;
|
||||||
|
|
||||||
|
assert.throws(() => { throw new Error('foo'); }, { bar: true });
|
14
test/message/assert_throws_stack.out
Normal file
14
test/message/assert_throws_stack.out
Normal file
@ -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 *
|
Loading…
x
Reference in New Issue
Block a user