From aa5a16a8ae19001b5153c133ec5d4a48cb5b1e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sat, 17 Sep 2016 18:25:03 +0200 Subject: [PATCH] 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 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Conflicts: test/parallel/test-buffer-deprecated.js test/parallel/test-repl-deprecated.js --- test/common.js | 13 +++++++++++++ test/parallel/test-crypto-deprecated.js | 13 ++----------- test/parallel/test-util.js | 13 ++----------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/test/common.js b/test/common.js index 5d226e29c5b6ff..69296c5e899f44 100644 --- a/test/common.js +++ b/test/common.js @@ -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)); +}; diff --git a/test/parallel/test-crypto-deprecated.js b/test/parallel/test-crypto-deprecated.js index a471b192e1ff45..903862d6c8ed27 100644 --- a/test/parallel/test-crypto-deprecated.js +++ b/test/parallel/test-crypto-deprecated.js @@ -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 diff --git a/test/parallel/test-util.js b/test/parallel/test-util.js index 6072f9ae9a249e..0f5ee28e37b59c 100644 --- a/test/parallel/test-util.js +++ b/test/parallel/test-util.js @@ -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');