From 8fb4ea9f75c8c82c46286bd5ca0c1115c4a5e956 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Tue, 20 Mar 2018 12:39:46 +0100 Subject: test: add deprecation code to expectWarning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a deprecation code to expectWarning and updates the function to check the passed code against the code property on the warning object. Not all warnings have a deprecation code so for those that don't an explicit code of common.noWarnCode is required. Passing this skips the assertion of the code. PR-URL: https://github.com/nodejs/node/pull/19474 Reviewed-By: Michaël Zasso Reviewed-By: James M Snell Reviewed-By: Tobias Nießen --- test/common/README.md | 10 ++++++++-- test/common/index.js | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 12 deletions(-) (limited to 'test/common') diff --git a/test/common/README.md b/test/common/README.md index 24468bdfd7..765ebe0123 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -108,11 +108,17 @@ Indicates if there is more than 1gb of total memory. returned function has not been called exactly `exact` number of times when the test is complete, then the test will fail. -### expectWarning(name, expected) +### expectWarning(name, expected, code) * `name` [<string>] * `expected` [<string>] | [<Array>] +* `code` [<string>] -Tests whether `name` and `expected` are part of a raised warning. +Tests whether `name`, `expected`, and `code` are part of a raised warning. If +an expected warning does not have a code then `common.noWarnCode` can be used +to indicate this. + +### noWarnCode +See `common.expectWarning()` for usage. ### fileExists(pathname) * pathname [<string>] diff --git a/test/common/index.js b/test/common/index.js index ff1dc0ce26..f13e61df8c 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -611,20 +611,32 @@ exports.isAlive = function isAlive(pid) { } }; -function expectWarning(name, expectedMessages) { +exports.noWarnCode = 'no_expected_warning_code'; + +function expectWarning(name, expected) { + const map = new Map(expected); return exports.mustCall((warning) => { assert.strictEqual(warning.name, name); - assert.ok(expectedMessages.includes(warning.message), + assert.ok(map.has(warning.message), `unexpected error message: "${warning.message}"`); + const code = map.get(warning.message); + if (code === undefined) { + throw new Error('An error code must be specified or use ' + + 'common.noWarnCode if there is no error code. The error ' + + `code for this warning was ${warning.code}`); + } + if (code !== exports.noWarnCode) { + assert.strictEqual(warning.code, code); + } // Remove a warning message after it is seen so that we guarantee that we // get each message only once. - expectedMessages.splice(expectedMessages.indexOf(warning.message), 1); - }, expectedMessages.length); + map.delete(expected); + }, map.size); } -function expectWarningByName(name, expected) { +function expectWarningByName(name, expected, code) { if (typeof expected === 'string') { - expected = [expected]; + expected = [[expected, code]]; } process.on('warning', expectWarning(name, expected)); } @@ -633,8 +645,15 @@ function expectWarningByMap(warningMap) { const catchWarning = {}; Object.keys(warningMap).forEach((name) => { let expected = warningMap[name]; - if (typeof expected === 'string') { - expected = [expected]; + 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); }); @@ -644,9 +663,9 @@ function expectWarningByMap(warningMap) { // 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) { +exports.expectWarning = function(nameOrMap, expected, code) { if (typeof nameOrMap === 'string') { - expectWarningByName(nameOrMap, expected); + expectWarningByName(nameOrMap, expected, code); } else { expectWarningByMap(nameOrMap); } -- cgit v1.2.3