Skip to content

Commit 2331c9b

Browse files
apexskierevanlucas
authored andcommitted
test: support multiple warnings in checkWarning
This allows the common.checkWarning() test method to accept a map of warning names to description(s), to allow testing code that generates multiple types of warnings. PR-URL: #11640 Reviewed-By: Anna Henningsen <[email protected]>
1 parent a7ffe48 commit 2331c9b

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

‎test/common.js‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,17 +554,45 @@ exports.isAlive = function isAlive(pid){
554554
}
555555
};
556556

557-
exports.expectWarning=function(name,expected){
558-
if(typeofexpected==='string')
559-
expected=[expected];
560-
process.on('warning',exports.mustCall((warning)=>{
557+
functionexpectWarning(name,expectedMessages){
558+
returnexports.mustCall((warning)=>{
561559
assert.strictEqual(warning.name,name);
562-
assert.ok(expected.includes(warning.message),
560+
assert.ok(expectedMessages.includes(warning.message),
563561
`unexpected error message: "${warning.message}"`);
564562
// Remove a warning message after it is seen so that we guarantee that we
565563
// get each message only once.
566-
expected.splice(expected.indexOf(warning.message),1);
567-
},expected.length));
564+
expectedMessages.splice(expectedMessages.indexOf(warning.message),1);
565+
},expectedMessages.length);
566+
}
567+
568+
functionexpectWarningByName(name,expected){
569+
if(typeofexpected==='string'){
570+
expected=[expected];
571+
}
572+
process.on('warning',expectWarning(name,expected));
573+
}
574+
575+
functionexpectWarningByMap(warningMap){
576+
constcatchWarning={};
577+
Object.keys(warningMap).forEach((name)=>{
578+
letexpected=warningMap[name];
579+
if(typeofexpected==='string'){
580+
expected=[expected];
581+
}
582+
catchWarning[name]=expectWarning(name,expected);
583+
});
584+
process.on('warning',(warning)=>catchWarning[warning.name](warning));
585+
}
586+
587+
// accepts a warning name and description or array of descriptions or a map
588+
// of warning names to description(s)
589+
// ensures a warning is generated for each name/description pair
590+
exports.expectWarning=function(nameOrMap,expected){
591+
if(typeofnameOrMap==='string'){
592+
expectWarningByName(nameOrMap,expected);
593+
}else{
594+
expectWarningByMap(nameOrMap);
595+
}
568596
};
569597

570598
Object.defineProperty(exports,'hasIntl',{

0 commit comments

Comments
(0)