Skip to content

Commit

Permalink
test: support multiple warnings in checkWarning
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
apexskier authored and evanlucas committed May 3, 2017
1 parent f60a2e9 commit d78adcc
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,45 @@ exports.isAlive = function isAlive(pid) {
}
};

exports.expectWarning = function(name, expected) {
if (typeof expected === 'string')
expected = [expected];
process.on('warning', exports.mustCall((warning) => {
function expectWarning(name, expectedMessages) {
return exports.mustCall((warning) => {
assert.strictEqual(warning.name, name);
assert.ok(expected.includes(warning.message),
assert.ok(expectedMessages.includes(warning.message),
`unexpected error message: "${warning.message}"`);
// Remove a warning message after it is seen so that we guarantee that we
// get each message only once.
expected.splice(expected.indexOf(warning.message), 1);
}, expected.length));
expectedMessages.splice(expectedMessages.indexOf(warning.message), 1);
}, expectedMessages.length);
}

function expectWarningByName(name, expected) {
if (typeof expected === 'string') {
expected = [expected];
}
process.on('warning', expectWarning(name, expected));
}

function expectWarningByMap(warningMap) {
const catchWarning = {};
Object.keys(warningMap).forEach((name) => {
let expected = warningMap[name];
if (typeof expected === 'string') {
expected = [expected];
}
catchWarning[name] = expectWarning(name, expected);
});
process.on('warning', (warning) => catchWarning[warning.name](warning));
}

// accepts a warning name and description or array of descriptions or a map
// of warning names to description(s)
// ensures a warning is generated for each name/description pair
exports.expectWarning = function(nameOrMap, expected) {
if (typeof nameOrMap === 'string') {
expectWarningByName(nameOrMap, expected);
} else {
expectWarningByMap(nameOrMap);
}
};

Object.defineProperty(exports, 'hasIntl', {
Expand Down

0 comments on commit d78adcc

Please sign in to comment.