Skip to content

Commit 881ebce

Browse files
BridgeARMylesBorins
authored andcommitted
assert: DRY .throws code
This refactors some code for less duplication. Backport-PR-URL: #31431 PR-URL: #28263 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e938784 commit 881ebce

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

lib/assert.js

+45-43
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
ObjectAssign,
2626
ObjectIs,
2727
ObjectKeys,
28+
ObjectPrototypeIsPrototypeOf,
2829
Map,
2930
} = primordials;
3031

@@ -571,6 +572,9 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
571572
}
572573

573574
function expectedException(actual, expected, message, fn) {
575+
let generatedMessage = false;
576+
let throwError = false;
577+
574578
if (typeof expected !== 'function') {
575579
// Handle regular expressions.
576580
if (isRegExp(expected)) {
@@ -583,20 +587,9 @@ function expectedException(actual, expected, message, fn) {
583587
message = 'The input did not match the regular expression ' +
584588
`${inspect(expected)}. Input:\n\n${inspect(str)}\n`;
585589
}
586-
587-
const err = new AssertionError({
588-
actual,
589-
expected,
590-
message,
591-
operator: fn.name,
592-
stackStartFn: fn
593-
});
594-
err.generatedMessage = generatedMessage;
595-
throw err;
596-
}
597-
598-
// Handle primitives properly.
599-
if (typeof actual !== 'object' || actual === null) {
590+
throwError = true;
591+
// Handle primitives properly.
592+
} else if (typeof actual !== 'object' || actual === null) {
600593
const err = new AssertionError({
601594
actual,
602595
expected,
@@ -606,43 +599,52 @@ function expectedException(actual, expected, message, fn) {
606599
});
607600
err.operator = fn.name;
608601
throw err;
609-
}
610-
611-
// Handle validation objects.
612-
const keys = ObjectKeys(expected);
613-
// Special handle errors to make sure the name and the message are compared
614-
// as well.
615-
if (expected instanceof Error) {
616-
keys.push('name', 'message');
617-
} else if (keys.length === 0) {
618-
throw new ERR_INVALID_ARG_VALUE('error',
619-
expected, 'may not be an empty object');
620-
}
621-
if (isDeepEqual === undefined) lazyLoadComparison();
622-
for (const key of keys) {
623-
if (typeof actual[key] === 'string' &&
624-
isRegExp(expected[key]) &&
625-
expected[key].test(actual[key])) {
626-
continue;
602+
} else {
603+
// Handle validation objects.
604+
const keys = ObjectKeys(expected);
605+
// Special handle errors to make sure the name and the message are
606+
// compared as well.
607+
if (expected instanceof Error) {
608+
keys.push('name', 'message');
609+
} else if (keys.length === 0) {
610+
throw new ERR_INVALID_ARG_VALUE('error',
611+
expected, 'may not be an empty object');
612+
}
613+
if (isDeepEqual === undefined) lazyLoadComparison();
614+
for (const key of keys) {
615+
if (typeof actual[key] === 'string' &&
616+
isRegExp(expected[key]) &&
617+
expected[key].test(actual[key])) {
618+
continue;
619+
}
620+
compareExceptionKey(actual, expected, key, message, keys, fn);
627621
}
628-
compareExceptionKey(actual, expected, key, message, keys, fn);
622+
return;
629623
}
630-
return;
631-
}
632-
633624
// Guard instanceof against arrow functions as they don't have a prototype.
634625
// Check for matching Error classes.
635-
if (expected.prototype !== undefined && actual instanceof expected) {
626+
} else if (expected.prototype !== undefined && actual instanceof expected) {
636627
return;
637-
}
638-
if (Error.isPrototypeOf(expected)) {
628+
} else if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
639629
throw actual;
630+
} else {
631+
// Check validation functions return value.
632+
const res = expected.call({}, actual);
633+
if (res !== true) {
634+
throw actual;
635+
}
640636
}
641637

642-
// Check validation functions return value.
643-
const res = expected.call({}, actual);
644-
if (res !== true) {
645-
throw actual;
638+
if (throwError) {
639+
const err = new AssertionError({
640+
actual,
641+
expected,
642+
message,
643+
operator: fn.name,
644+
stackStartFn: fn
645+
});
646+
err.generatedMessage = generatedMessage;
647+
throw err;
646648
}
647649
}
648650

0 commit comments

Comments
 (0)