Skip to content

Commit 84948cf

Browse files
BridgeARMylesBorins
authored andcommitted
assert: fix .throws operator
assert.throws and assert.doesNotThrow set the operator to a internal function. That was by accident and originally the operator was undefined. This changes it to show "throws" or "doesNotThrow". Backport-PR-URL: #23223 PR-URL: #17575 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent c6d94f8 commit 84948cf

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

lib/assert.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -643,15 +643,18 @@ function innerThrows(shouldThrow, block, expected, message) {
643643
details += ` (${expected.name})`;
644644
}
645645
details += message ? `: ${message}` : '.';
646-
fail(actual, expected, `Missing expected exception${details}`, fail);
646+
fail(actual, expected, `Missing expected exception${details}`, 'throws');
647647
}
648648
if (expected && expectedException(actual, expected) === false) {
649649
throw actual;
650650
}
651651
} else if (actual !== undefined) {
652652
if (!expected || expectedException(actual, expected)) {
653653
details = message ? `: ${message}` : '.';
654-
fail(actual, expected, `Got unwanted exception${details}`, fail);
654+
fail(actual,
655+
expected,
656+
`Got unwanted exception${details}`,
657+
'doesNotThrow');
655658
}
656659
throw actual;
657660
}

test/parallel/test-assert.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,15 @@ assert.throws(() => { assert.ifError(new Error('test error')); },
463463
assert.doesNotThrow(() => { assert.ifError(null); });
464464
assert.doesNotThrow(() => { assert.ifError(); });
465465

466-
assert.throws(() => {
467-
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
468-
}, /Got unwanted exception: user message/,
469-
'a.doesNotThrow ignores user message');
466+
common.expectsError(
467+
() => assert.doesNotThrow(makeBlock(thrower, Error), 'user message'),
468+
{
469+
type: a.AssertionError,
470+
code: 'ERR_ASSERTION',
471+
operator: 'doesNotThrow',
472+
message: 'Got unwanted exception: user message\n[object Object]'
473+
}
474+
);
470475

471476
// make sure that validating using constructor really works
472477
{
@@ -525,7 +530,8 @@ a.throws(makeBlock(thrower, TypeError), (err) => {
525530
() => { a.throws((noop)); },
526531
common.expectsError({
527532
code: 'ERR_ASSERTION',
528-
message: /^Missing expected exception\.$/
533+
message: /^Missing expected exception\.$/,
534+
operator: 'throws'
529535
}));
530536

531537
assert.throws(

0 commit comments

Comments
 (0)