From 85f58a94a968fa60fd11428205ec155c59246c15 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 --- test/common.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/common.js b/test/common.js index a4089183eff727..a72560be77a3a9 100644 --- a/test/common.js +++ b/test/common.js @@ -533,3 +533,17 @@ ArrayStream.prototype.writable = true; ArrayStream.prototype.pause = function() {}; ArrayStream.prototype.resume = function() {}; ArrayStream.prototype.write = function() {}; + +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)); +}; +