Skip to content

Commit

Permalink
test: add expectWarning to common
Browse files Browse the repository at this point in the history
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
  • Loading branch information
targos authored and Fishrock123 committed Oct 11, 2016
1 parent b46d8cd commit aa5a16a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
13 changes: 13 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,16 @@ exports.isAlive = function isAlive(pid) {
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));
};
13 changes: 2 additions & 11 deletions test/parallel/test-crypto-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@ if (!common.hasCrypto) {
const crypto = require('crypto');
const tls = require('tls');

const expected = [
common.expectWarning('DeprecationWarning', [
'crypto.Credentials is deprecated. Use tls.SecureContext 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.
// It does not need to be called. So the assert serves the purpose of both
Expand Down
13 changes: 2 additions & 11 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,12 @@ assert.strictEqual(util.isFunction(function() {}), true);
assert.strictEqual(util.isFunction(), false);
assert.strictEqual(util.isFunction('string'), false);

const expected = [
common.expectWarning('DeprecationWarning', [
'util.print is deprecated. Use console.log instead.',
'util.puts is deprecated. Use console.log instead.',
'util.debug 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.puts('test');
Expand Down

0 comments on commit aa5a16a

Please sign in to comment.