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

1 file changed

Lines changed: 40 additions & 57 deletions

File tree

‎lib/assert.js‎

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

564564
functionexpectedException(actual,expected,message,fn){
565565
letgeneratedMessage=false;
566+
letthrowError=false;
566567

567568
if(typeofexpected!=='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-
consterr=newAssertionError({
581-
actual,
582-
expected,
583-
message,
584-
operator: fn.name,
585-
stackStartFn: fn
586-
});
587-
err.generatedMessage=generatedMessage;
588-
throwerr;
589-
}
590-
591-
// Handle primitives properly.
592-
if(typeofactual!=='object'||actual===null){
580+
throwError=true;
581+
// Handle primitives properly.
582+
}elseif(typeofactual!=='object'||actual===null){
593583
consterr=newAssertionError({
594584
actual,
595585
expected,
@@ -599,36 +589,33 @@ function expectedException(actual, expected, message, fn) {
599589
});
600590
err.operator=fn.name;
601591
throwerr;
602-
}
603-
604-
// Handle validation objects.
605-
constkeys=Object.keys(expected);
606-
// Special handle errors to make sure the name and the message are compared
607-
// as well.
608-
if(expectedinstanceofError){
609-
keys.push('name','message');
610-
}elseif(keys.length===0){
611-
thrownewERR_INVALID_ARG_VALUE('error',
612-
expected,'may not be an empty object');
613-
}
614-
if(isDeepEqual===undefined)lazyLoadComparison();
615-
for(constkeyofkeys){
616-
if(typeofactual[key]==='string'&&
617-
isRegExp(expected[key])&&
618-
expected[key].test(actual[key])){
619-
continue;
592+
}else{
593+
// Handle validation objects.
594+
constkeys=Object.keys(expected);
595+
// Special handle errors to make sure the name and the message are
596+
// compared as well.
597+
if(expectedinstanceofError){
598+
keys.push('name','message');
599+
}elseif(keys.length===0){
600+
thrownewERR_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(constkeyofkeys){
605+
if(typeofactual[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&&actualinstanceofexpected){
616+
}elseif(expected.prototype!==undefined&&actualinstanceofexpected){
629617
return;
630-
}
631-
if(ObjectPrototype.isPrototypeOf(Error,expected)){
618+
}elseif(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-
consterr=newAssertionError({
643-
actual,
644-
expected,
645-
message,
646-
operator: fn.name,
647-
stackStartFn: fn
648-
});
649-
err.generatedMessage=generatedMessage;
650-
throwerr;
629+
throwError=true;
630+
}else{
631+
// Check validation functions return value.
632+
constres=expected.call({},actual);
633+
if(res!==true){
634+
if(!message){
635+
generatedMessage=true;
636+
constname=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-
constres=expected.call({},actual);
655-
if(res!==true){
656-
if(!message){
657-
generatedMessage=true;
658-
constname=expected.name ? `"${expected.name}" ` : '';
659-
message=`The ${name}validation function is expected to return "true".`+
660-
` Received ${inspect(res)}`;
661-
}
644+
if(throwError){
662645
consterr=newAssertionError({
663646
actual,
664647
expected,

0 commit comments

Comments
 (0)