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

+35-7
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 (typeof expected === 'string')
559-
expected = [expected];
560-
process.on('warning', exports.mustCall((warning) => {
557+
function expectWarning(name, expectedMessages) {
558+
return exports.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+
function expectWarningByName(name, expected) {
569+
if (typeof expected === 'string') {
570+
expected = [expected];
571+
}
572+
process.on('warning', expectWarning(name, expected));
573+
}
574+
575+
function expectWarningByMap(warningMap) {
576+
const catchWarning = {};
577+
Object.keys(warningMap).forEach((name) => {
578+
let expected = warningMap[name];
579+
if (typeof expected === '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 (typeof nameOrMap === 'string') {
592+
expectWarningByName(nameOrMap, expected);
593+
} else {
594+
expectWarningByMap(nameOrMap);
595+
}
568596
};
569597

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

0 commit comments

Comments
 (0)