Skip to content

Commit

Permalink
assert: fix .throws operator
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
BridgeAR authored and MylesBorins committed Oct 31, 2018
1 parent c6d94f8 commit 84948cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/assert.js
Expand Up @@ -643,15 +643,18 @@ function innerThrows(shouldThrow, block, expected, message) {
details += ` (${expected.name})`;
}
details += message ? `: ${message}` : '.';
fail(actual, expected, `Missing expected exception${details}`, fail);
fail(actual, expected, `Missing expected exception${details}`, 'throws');
}
if (expected && expectedException(actual, expected) === false) {
throw actual;
}
} else if (actual !== undefined) {
if (!expected || expectedException(actual, expected)) {
details = message ? `: ${message}` : '.';
fail(actual, expected, `Got unwanted exception${details}`, fail);
fail(actual,
expected,
`Got unwanted exception${details}`,
'doesNotThrow');
}
throw actual;
}
Expand Down
16 changes: 11 additions & 5 deletions test/parallel/test-assert.js
Expand Up @@ -463,10 +463,15 @@ assert.throws(() => { assert.ifError(new Error('test error')); },
assert.doesNotThrow(() => { assert.ifError(null); });
assert.doesNotThrow(() => { assert.ifError(); });

assert.throws(() => {
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
}, /Got unwanted exception: user message/,
'a.doesNotThrow ignores user message');
common.expectsError(
() => assert.doesNotThrow(makeBlock(thrower, Error), 'user message'),
{
type: a.AssertionError,
code: 'ERR_ASSERTION',
operator: 'doesNotThrow',
message: 'Got unwanted exception: user message\n[object Object]'
}
);

// make sure that validating using constructor really works
{
Expand Down Expand Up @@ -525,7 +530,8 @@ a.throws(makeBlock(thrower, TypeError), (err) => {
() => { a.throws((noop)); },
common.expectsError({
code: 'ERR_ASSERTION',
message: /^Missing expected exception\.$/
message: /^Missing expected exception\.$/,
operator: 'throws'
}));

assert.throws(
Expand Down

0 comments on commit 84948cf

Please sign in to comment.