assert: improve assert.throws · nodejs/node@147aeed · GitHub
Skip to content

Commit 147aeed

Browse files
BridgeARMylesBorins
authored andcommitted
assert: improve assert.throws
Throw a TypeError in case a error message is provided in the second argument and a third argument is present as well. This is clearly a mistake and should not be done. Backport-PR-URL: #23223 PR-URL: #17585 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
1 parent c9d84b6 commit 147aeed

3 files changed

Lines changed: 93 additions & 52 deletions

File tree

doc/api/assert.md

Lines changed: 31 additions & 6 deletions

lib/assert.js

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -675,68 +675,73 @@ function expectedException(actual, expected) {
675675
return expected.call({}, actual) === true;
676676
}
677677

678-
function tryBlock(block) {
678+
function getActual(block) {
679+
if (typeof block !== 'function') {
680+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
681+
block);
682+
}
679683
try {
680684
block();
681685
} catch (e) {
682686
return e;
683687
}
684688
}
685689

686-
function innerThrows(shouldThrow, block, expected, message) {
687-
var details = '';
690+
// Expected to throw an error.
691+
assert.throws = function throws(block, error, message) {
692+
const actual = getActual(block);
688693

689-
if (typeof block !== 'function') {
690-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'function',
691-
block);
692-
}
694+
if (typeof error === 'string') {
695+
if (arguments.length === 3)
696+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
697+
'error',
698+
['Function', 'RegExp'],
699+
error);
693700

694-
if (typeof expected === 'string') {
695-
message = expected;
696-
expected = null;
701+
message = error;
702+
error = null;
697703
}
698704

699-
const actual = tryBlock(block);
700-
701-
if (shouldThrow === true) {
702-
if (actual === undefined) {
703-
if (expected && expected.name) {
704-
details += ` (${expected.name})`;
705-
}
706-
details += message ? `: ${message}` : '.';
707-
innerFail({
708-
actual,
709-
expected,
710-
operator: 'throws',
711-
message: `Missing expected exception${details}`,
712-
stackStartFn: assert.throws
713-
});
714-
}
715-
if (expected && expectedException(actual, expected) === false) {
716-
throw actual;
717-
}
718-
} else if (actual !== undefined) {
719-
if (!expected || expectedException(actual, expected)) {
720-
details = message ? `: ${message}` : '.';
721-
innerFail({
722-
actual,
723-
expected,
724-
operator: 'doesNotThrow',
725-
message: `Got unwanted exception${details}`,
726-
stackStartFn: assert.doesNotThrow
727-
});
705+
if (actual === undefined) {
706+
let details = '';
707+
if (error && error.name) {
708+
details += ` (${error.name})`;
728709
}
710+
details += message ? `: ${message}` : '.';
711+
innerFail({
712+
actual,
713+
expected: error,
714+
operator: 'throws',
715+
message: `Missing expected exception${details}`,
716+
stackStartFn: throws
717+
});
718+
}
719+
if (error && expectedException(actual, error) === false) {
729720
throw actual;
730721
}
731-
}
732-
733-
// Expected to throw an error.
734-
assert.throws = function throws(block, error, message) {
735-
innerThrows(true, block, error, message);
736722
};
737723

738724
assert.doesNotThrow = function doesNotThrow(block, error, message) {
739-
innerThrows(false, block, error, message);
725+
const actual = getActual(block);
726+
if (actual === undefined)
727+
return;
728+
729+
if (typeof error === 'string') {
730+
message = error;
731+
error = null;
732+
}
733+
734+
if (!error || expectedException(actual, error)) {
735+
const details = message ? `: ${message}` : '.';
736+
innerFail({
737+
actual,
738+
expected: error,
739+
operator: 'doesNotThrow',
740+
message: `Got unwanted exception${details}\n${actual.message}`,
741+
stackStartFn: doesNotThrow
742+
});
743+
}
744+
throw actual;
740745
};
741746

742747
assert.ifError = function ifError(err) { if (err) throw err; };

test/parallel/test-assert.js

Lines changed: 12 additions & 1 deletion

0 commit comments

Comments
 (0)