Skip to content

Commit 74f61e8

Browse files
apexskieraddaleax
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 765be6c commit 74f61e8

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

test/common.js

+35-7
Original file line numberDiff line numberDiff line change
@@ -583,17 +583,45 @@ exports.isAlive = function isAlive(pid) {
583583
}
584584
};
585585

586-
exports.expectWarning = function(name, expected) {
587-
if (typeof expected === 'string')
588-
expected = [expected];
589-
process.on('warning', exports.mustCall((warning) => {
586+
function expectWarning(name, expectedMessages) {
587+
return exports.mustCall((warning) => {
590588
assert.strictEqual(warning.name, name);
591-
assert.ok(expected.includes(warning.message),
589+
assert.ok(expectedMessages.includes(warning.message),
592590
`unexpected error message: "${warning.message}"`);
593591
// Remove a warning message after it is seen so that we guarantee that we
594592
// get each message only once.
595-
expected.splice(expected.indexOf(warning.message), 1);
596-
}, expected.length));
593+
expectedMessages.splice(expectedMessages.indexOf(warning.message), 1);
594+
}, expectedMessages.length);
595+
}
596+
597+
function expectWarningByName(name, expected) {
598+
if (typeof expected === 'string') {
599+
expected = [expected];
600+
}
601+
process.on('warning', expectWarning(name, expected));
602+
}
603+
604+
function expectWarningByMap(warningMap) {
605+
const catchWarning = {};
606+
Object.keys(warningMap).forEach((name) => {
607+
let expected = warningMap[name];
608+
if (typeof expected === 'string') {
609+
expected = [expected];
610+
}
611+
catchWarning[name] = expectWarning(name, expected);
612+
});
613+
process.on('warning', (warning) => catchWarning[warning.name](warning));
614+
}
615+
616+
// accepts a warning name and description or array of descriptions or a map
617+
// of warning names to description(s)
618+
// ensures a warning is generated for each name/description pair
619+
exports.expectWarning = function(nameOrMap, expected) {
620+
if (typeof nameOrMap === 'string') {
621+
expectWarningByName(nameOrMap, expected);
622+
} else {
623+
expectWarningByMap(nameOrMap);
624+
}
597625
};
598626

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

0 commit comments

Comments
 (0)