summaryrefslogtreecommitdiff
path: root/test/common
diff options
context:
space:
mode:
Diffstat (limited to 'test/common')
-rw-r--r--test/common/README.md10
-rw-r--r--test/common/index.js39
2 files changed, 37 insertions, 12 deletions
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);
}