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 <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e938784 commit 881ebce

1 file changed

Lines changed: 45 additions & 43 deletions

File tree

‎lib/assert.js‎

Lines changed: 45 additions & 43 deletions
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
functionexpectedException(actual,expected,message,fn){
575+
letgeneratedMessage=false;
576+
letthrowError=false;
577+
574578
if(typeofexpected!=='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-
consterr=newAssertionError({
588-
actual,
589-
expected,
590-
message,
591-
operator: fn.name,
592-
stackStartFn: fn
593-
});
594-
err.generatedMessage=generatedMessage;
595-
throwerr;
596-
}
597-
598-
// Handle primitives properly.
599-
if(typeofactual!=='object'||actual===null){
590+
throwError=true;
591+
// Handle primitives properly.
592+
}elseif(typeofactual!=='object'||actual===null){
600593
consterr=newAssertionError({
601594
actual,
602595
expected,
@@ -606,43 +599,52 @@ function expectedException(actual, expected, message, fn) {
606599
});
607600
err.operator=fn.name;
608601
throwerr;
609-
}
610-
611-
// Handle validation objects.
612-
constkeys=ObjectKeys(expected);
613-
// Special handle errors to make sure the name and the message are compared
614-
// as well.
615-
if(expectedinstanceofError){
616-
keys.push('name','message');
617-
}elseif(keys.length===0){
618-
thrownewERR_INVALID_ARG_VALUE('error',
619-
expected,'may not be an empty object');
620-
}
621-
if(isDeepEqual===undefined)lazyLoadComparison();
622-
for(constkeyofkeys){
623-
if(typeofactual[key]==='string'&&
624-
isRegExp(expected[key])&&
625-
expected[key].test(actual[key])){
626-
continue;
602+
}else{
603+
// Handle validation objects.
604+
constkeys=ObjectKeys(expected);
605+
// Special handle errors to make sure the name and the message are
606+
// compared as well.
607+
if(expectedinstanceofError){
608+
keys.push('name','message');
609+
}elseif(keys.length===0){
610+
thrownewERR_INVALID_ARG_VALUE('error',
611+
expected,'may not be an empty object');
612+
}
613+
if(isDeepEqual===undefined)lazyLoadComparison();
614+
for(constkeyofkeys){
615+
if(typeofactual[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&&actualinstanceofexpected){
626+
}elseif(expected.prototype!==undefined&&actualinstanceofexpected){
636627
return;
637-
}
638-
if(Error.isPrototypeOf(expected)){
628+
}elseif(ObjectPrototypeIsPrototypeOf(Error,expected)){
639629
throwactual;
630+
}else{
631+
// Check validation functions return value.
632+
constres=expected.call({},actual);
633+
if(res!==true){
634+
throwactual;
635+
}
640636
}
641637

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

0 commit comments

Comments
 (0)