Skip to content

Commit 5700cd1

Browse files
committed
assert: do not repeat .throws() code
This refactors some code for less duplication. PR-URL: #28263 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d47b678 commit 5700cd1

File tree

1 file changed

+40
-57
lines changed

1 file changed

+40
-57
lines changed

lib/assert.js

+40-57
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
563563

564564
function expectedException(actual, expected, message, fn) {
565565
let generatedMessage = false;
566+
let throwError = false;
566567

567568
if (typeof expected !== 'function') {
568569
// Handle regular expressions.
@@ -576,20 +577,9 @@ function expectedException(actual, expected, message, fn) {
576577
message = 'The input did not match the regular expression ' +
577578
`${inspect(expected)}. Input:\n\n${inspect(str)}\n`;
578579
}
579-
580-
const err = new AssertionError({
581-
actual,
582-
expected,
583-
message,
584-
operator: fn.name,
585-
stackStartFn: fn
586-
});
587-
err.generatedMessage = generatedMessage;
588-
throw err;
589-
}
590-
591-
// Handle primitives properly.
592-
if (typeof actual !== 'object' || actual === null) {
580+
throwError = true;
581+
// Handle primitives properly.
582+
} else if (typeof actual !== 'object' || actual === null) {
593583
const err = new AssertionError({
594584
actual,
595585
expected,
@@ -599,36 +589,33 @@ function expectedException(actual, expected, message, fn) {
599589
});
600590
err.operator = fn.name;
601591
throw err;
602-
}
603-
604-
// Handle validation objects.
605-
const keys = Object.keys(expected);
606-
// Special handle errors to make sure the name and the message are compared
607-
// as well.
608-
if (expected instanceof Error) {
609-
keys.push('name', 'message');
610-
} else if (keys.length === 0) {
611-
throw new ERR_INVALID_ARG_VALUE('error',
612-
expected, 'may not be an empty object');
613-
}
614-
if (isDeepEqual === undefined) lazyLoadComparison();
615-
for (const key of keys) {
616-
if (typeof actual[key] === 'string' &&
617-
isRegExp(expected[key]) &&
618-
expected[key].test(actual[key])) {
619-
continue;
592+
} else {
593+
// Handle validation objects.
594+
const keys = Object.keys(expected);
595+
// Special handle errors to make sure the name and the message are
596+
// compared as well.
597+
if (expected instanceof Error) {
598+
keys.push('name', 'message');
599+
} else if (keys.length === 0) {
600+
throw new ERR_INVALID_ARG_VALUE('error',
601+
expected, 'may not be an empty object');
620602
}
621-
compareExceptionKey(actual, expected, key, message, keys, fn);
603+
if (isDeepEqual === undefined) lazyLoadComparison();
604+
for (const key of keys) {
605+
if (typeof actual[key] === 'string' &&
606+
isRegExp(expected[key]) &&
607+
expected[key].test(actual[key])) {
608+
continue;
609+
}
610+
compareExceptionKey(actual, expected, key, message, keys, fn);
611+
}
612+
return;
622613
}
623-
return;
624-
}
625-
626614
// Guard instanceof against arrow functions as they don't have a prototype.
627615
// Check for matching Error classes.
628-
if (expected.prototype !== undefined && actual instanceof expected) {
616+
} else if (expected.prototype !== undefined && actual instanceof expected) {
629617
return;
630-
}
631-
if (ObjectPrototype.isPrototypeOf(Error, expected)) {
618+
} else if (ObjectPrototype.isPrototypeOf(Error, expected)) {
632619
if (!message) {
633620
generatedMessage = true;
634621
message = 'The error is expected to be an instance of ' +
@@ -639,26 +626,22 @@ function expectedException(actual, expected, message, fn) {
639626
message += `"${inspect(actual, { depth: -1 })}"`;
640627
}
641628
}
642-
const err = new AssertionError({
643-
actual,
644-
expected,
645-
message,
646-
operator: fn.name,
647-
stackStartFn: fn
648-
});
649-
err.generatedMessage = generatedMessage;
650-
throw err;
629+
throwError = true;
630+
} else {
631+
// Check validation functions return value.
632+
const res = expected.call({}, actual);
633+
if (res !== true) {
634+
if (!message) {
635+
generatedMessage = true;
636+
const name = expected.name ? `"${expected.name}" ` : '';
637+
message = `The ${name}validation function is expected to return` +
638+
` "true". Received ${inspect(res)}`;
639+
}
640+
throwError = true;
641+
}
651642
}
652643

653-
// Check validation functions return value.
654-
const res = expected.call({}, actual);
655-
if (res !== true) {
656-
if (!message) {
657-
generatedMessage = true;
658-
const name = expected.name ? `"${expected.name}" ` : '';
659-
message = `The ${name}validation function is expected to return "true".` +
660-
` Received ${inspect(res)}`;
661-
}
644+
if (throwError) {
662645
const err = new AssertionError({
663646
actual,
664647
expected,

0 commit comments

Comments
 (0)