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 <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
1 parent c6d94f8 commit 84948cf

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

‎lib/assert.js‎

Lines changed: 5 additions & 2 deletions
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
throwactual;
650650
}
651651
}elseif(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
throwactual;
657660
}

‎test/parallel/test-assert.js‎

Lines changed: 11 additions & 5 deletions
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-
},/Gotunwantedexception:usermessage/,
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: /^Missingexpectedexception\.$/
533+
message: /^Missingexpectedexception\.$/,
534+
operator: 'throws'
529535
}));
530536

531537
assert.throws(

0 commit comments

Comments
 (0)