Skip to content

Commit aa5a16a

Browse files
targosFishrock123
authored andcommitted
test: add expectWarning to common
There are multiple tests that use the same boilerplate to test that warnings are correctly emitted. This adds a new common function to do that and changes the tests to use it. PR-URL: #8662 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Conflicts: test/parallel/test-buffer-deprecated.js test/parallel/test-repl-deprecated.js
1 parent b46d8cd commit aa5a16a

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

test/common.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,16 @@ exports.isAlive = function isAlive(pid) {
508508
return false;
509509
}
510510
};
511+
512+
exports.expectWarning = function(name, expected) {
513+
if (typeof expected === 'string')
514+
expected = [expected];
515+
process.on('warning', exports.mustCall((warning) => {
516+
assert.strictEqual(warning.name, name);
517+
assert.ok(expected.includes(warning.message),
518+
`unexpected error message: "${warning.message}"`);
519+
// Remove a warning message after it is seen so that we guarantee that we
520+
// get each message only once.
521+
expected.splice(expected.indexOf(warning.message), 1);
522+
}, expected.length));
523+
};

test/parallel/test-crypto-deprecated.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,10 @@ if (!common.hasCrypto) {
99
const crypto = require('crypto');
1010
const tls = require('tls');
1111

12-
const expected = [
12+
common.expectWarning('DeprecationWarning', [
1313
'crypto.Credentials is deprecated. Use tls.SecureContext instead.',
1414
'crypto.createCredentials is deprecated. Use tls.createSecureContext instead.'
15-
];
16-
17-
process.on('warning', common.mustCall((warning) => {
18-
assert.strictEqual(warning.name, 'DeprecationWarning');
19-
assert.notStrictEqual(expected.indexOf(warning.message), -1,
20-
`unexpected error message: "${warning.message}"`);
21-
// Remove a warning message after it is seen so that we guarantee that we get
22-
// each message only once.
23-
expected.splice(expected.indexOf(warning.message), 1);
24-
}, expected.length));
15+
]);
2516

2617
// Accessing the deprecated function is enough to trigger the warning event.
2718
// It does not need to be called. So the assert serves the purpose of both

test/parallel/test-util.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,12 @@ assert.strictEqual(util.isFunction(function() {}), true);
121121
assert.strictEqual(util.isFunction(), false);
122122
assert.strictEqual(util.isFunction('string'), false);
123123

124-
const expected = [
124+
common.expectWarning('DeprecationWarning', [
125125
'util.print is deprecated. Use console.log instead.',
126126
'util.puts is deprecated. Use console.log instead.',
127127
'util.debug is deprecated. Use console.error instead.',
128128
'util.error is deprecated. Use console.error instead.'
129-
];
130-
131-
process.on('warning', common.mustCall((warning) => {
132-
assert.strictEqual(warning.name, 'DeprecationWarning');
133-
assert.notStrictEqual(expected.indexOf(warning.message), -1,
134-
`unexpected error message: "${warning.message}"`);
135-
// Remove a warning message after it is seen so that we guarantee that we get
136-
// each message only once.
137-
expected.splice(expected.indexOf(warning.message), 1);
138-
}, expected.length));
129+
]);
139130

140131
util.print('test');
141132
util.puts('test');

0 commit comments

Comments
 (0)