Skip to content

Commit cccddc5

Browse files
BridgeARapapirovski
authored andcommitted
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: nodejs#18595 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
1 parent 14bc3e2 commit cccddc5

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

lib/assert.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,17 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
361361
}
362362
};
363363

364-
function createMsg(msg, key, actual, expected) {
365-
if (msg)
366-
return msg;
367-
return `${key}: expected ${inspect(expected[key])}, ` +
368-
`not ${inspect(actual[key])}`;
364+
function compareExceptionKey(actual, expected, key, msg) {
365+
if (!isDeepStrictEqual(actual[key], expected[key])) {
366+
innerFail({
367+
actual: actual[key],
368+
expected: expected[key],
369+
message: msg || `${key}: expected ${inspect(expected[key])}, ` +
370+
`not ${inspect(actual[key])}`,
371+
operator: 'throws',
372+
stackStartFn: assert.throws
373+
});
374+
}
369375
}
370376

371377
function expectedException(actual, expected, msg) {
@@ -380,23 +386,13 @@ function expectedException(actual, expected, msg) {
380386
// The name and message could be non enumerable. Therefore test them
381387
// explicitly.
382388
if ('name' in expected) {
383-
assert.strictEqual(
384-
actual.name,
385-
expected.name,
386-
createMsg(msg, 'name', actual, expected));
389+
compareExceptionKey(actual, expected, 'name', msg);
387390
}
388391
if ('message' in expected) {
389-
assert.strictEqual(
390-
actual.message,
391-
expected.message,
392-
createMsg(msg, 'message', actual, expected));
392+
compareExceptionKey(actual, expected, 'message', msg);
393393
}
394-
const keys = Object.keys(expected);
395-
for (const key of keys) {
396-
assert.deepStrictEqual(
397-
actual[key],
398-
expected[key],
399-
createMsg(msg, key, actual, expected));
394+
for (const key of Object.keys(expected)) {
395+
compareExceptionKey(actual, expected, key, msg);
400396
}
401397
return true;
402398
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert').strict;
5+
6+
assert.throws(() => { throw new Error('foo'); }, { bar: true });
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
assert.js:*
2+
throw new AssertionError(obj);
3+
^
4+
5+
AssertionError [ERR_ASSERTION]: bar: expected true, not undefined
6+
at Object.<anonymous> (*assert_throws_stack.js:*:*)
7+
at *
8+
at *
9+
at *
10+
at *
11+
at *
12+
at *
13+
at *
14+
at *

0 commit comments

Comments
 (0)