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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Michaël Zasso 2016-09-17 18:25:03 +02:00
parent c063502566
commit 43e1ca84f2
5 changed files with 20 additions and 41 deletions

View File

@ -508,3 +508,16 @@ exports.isAlive = function isAlive(pid) {
return false; return false;
} }
}; };
exports.expectWarning = function(name, expected) {
if (typeof expected === 'string')
expected = [expected];
process.on('warning', exports.mustCall((warning) => {
assert.strictEqual(warning.name, name);
assert.ok(expected.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));
};

View File

@ -1,17 +1,11 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const expected = const expected =
'Using Buffer without `new` will soon stop working. ' + 'Using Buffer without `new` will soon stop working. ' +
'Use `new Buffer()`, or preferably ' + 'Use `new Buffer()`, or preferably ' +
'`Buffer.from()`, `Buffer.allocUnsafe()` or `Buffer.alloc()` instead.'; '`Buffer.from()`, `Buffer.allocUnsafe()` or `Buffer.alloc()` instead.';
common.expectWarning('DeprecationWarning', expected);
process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.strictEqual(warning.message, expected,
`unexpected error message: "${warning.message}"`);
}, 1));
Buffer(1); Buffer(1);
Buffer(1); Buffer(1);

View File

@ -9,19 +9,10 @@ if (!common.hasCrypto) {
const crypto = require('crypto'); const crypto = require('crypto');
const tls = require('tls'); const tls = require('tls');
const expected = [ common.expectWarning('DeprecationWarning', [
'crypto.Credentials is deprecated. Use tls.SecureContext instead.', 'crypto.Credentials is deprecated. Use tls.SecureContext instead.',
'crypto.createCredentials is deprecated. Use tls.createSecureContext instead.' 'crypto.createCredentials is deprecated. Use tls.createSecureContext instead.'
]; ]);
process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.notStrictEqual(expected.indexOf(warning.message), -1,
`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));
// Accessing the deprecated function is enough to trigger the warning event. // Accessing the deprecated function is enough to trigger the warning event.
// It does not need to be called. So the assert serves the purpose of both // It does not need to be called. So the assert serves the purpose of both

View File

@ -3,18 +3,8 @@ const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const repl = require('repl'); const repl = require('repl');
const expected = [ common.expectWarning('DeprecationWarning',
'replServer.convertToContext() is deprecated' 'replServer.convertToContext() is deprecated');
];
process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.notStrictEqual(expected.indexOf(warning.message), -1,
`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));
// Create a dummy stream that does nothing // Create a dummy stream that does nothing
const stream = new common.ArrayStream(); const stream = new common.ArrayStream();

View File

@ -121,21 +121,12 @@ assert.strictEqual(util.isFunction(function() {}), true);
assert.strictEqual(util.isFunction(), false); assert.strictEqual(util.isFunction(), false);
assert.strictEqual(util.isFunction('string'), false); assert.strictEqual(util.isFunction('string'), false);
const expected = [ common.expectWarning('DeprecationWarning', [
'util.print is deprecated. Use console.log instead.', 'util.print is deprecated. Use console.log instead.',
'util.puts is deprecated. Use console.log instead.', 'util.puts is deprecated. Use console.log instead.',
'util.debug is deprecated. Use console.error instead.', 'util.debug is deprecated. Use console.error instead.',
'util.error is deprecated. Use console.error instead.' 'util.error is deprecated. Use console.error instead.'
]; ]);
process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.notStrictEqual(expected.indexOf(warning.message), -1,
`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));
util.print('test'); util.print('test');
util.puts('test'); util.puts('test');