diff options
Diffstat (limited to 'test/common/index.js')
-rw-r--r-- | test/common/index.js | 66 |
1 files changed, 27 insertions, 39 deletions
diff --git a/test/common/index.js b/test/common/index.js index 5d9a4b31e3..ea59009ba5 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -497,54 +497,43 @@ function isAlive(pid) { } } -function _expectWarning(name, expected) { - const map = new Map(expected); +function _expectWarning(name, expected, code) { + if (typeof expected === 'string') { + expected = [[expected, code]]; + } else if (!Array.isArray(expected)) { + expected = Object.entries(expected).map(([a, b]) => [b, a]); + } else if (!(Array.isArray(expected[0]))) { + expected = [[expected[0], expected[1]]]; + } + // Deprecation codes are mandatory, everything else is not. + if (name === 'DeprecationWarning') { + expected.forEach(([_, code]) => assert(code, expected)); + } return mustCall((warning) => { + const [ message, code ] = expected.shift(); assert.strictEqual(warning.name, name); - assert.ok(map.has(warning.message), - `unexpected error message: "${warning.message}"`); - const code = map.get(warning.message); + assert.strictEqual(warning.message, message); assert.strictEqual(warning.code, code); - // Remove a warning message after it is seen so that we guarantee that we - // get each message only once. - map.delete(expected); }, expected.length); } -function expectWarningByName(name, expected, code) { - if (typeof expected === 'string') { - expected = [[expected, code]]; - } - process.on('warning', _expectWarning(name, expected)); -} +let catchWarning; -function expectWarningByMap(warningMap) { - const catchWarning = {}; - Object.keys(warningMap).forEach((name) => { - let expected = warningMap[name]; - if (!Array.isArray(expected)) { - throw new Error('warningMap entries must be arrays consisting of two ' + - 'entries: [message, warningCode]'); - } - if (!(Array.isArray(expected[0]))) { - if (expected.length === 0) { - return; - } - expected = [[expected[0], expected[1]]]; - } - 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 +// 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. +// The expected messages have to be unique per `expectWarning()` call. function expectWarning(nameOrMap, expected, code) { + if (catchWarning === undefined) { + catchWarning = {}; + process.on('warning', (warning) => catchWarning[warning.name](warning)); + } if (typeof nameOrMap === 'string') { - expectWarningByName(nameOrMap, expected, code); + catchWarning[nameOrMap] = _expectWarning(nameOrMap, expected, code); } else { - expectWarningByMap(nameOrMap); + Object.keys(nameOrMap).forEach((name) => { + catchWarning[name] = _expectWarning(name, nameOrMap[name]); + }); } } @@ -758,7 +747,6 @@ module.exports = { mustCallAtLeast, mustNotCall, nodeProcessAborted, - noWarnCode: undefined, PIPE, platformTimeout, printSkipMessage, |