From 318328f6b75915c0185d4a4cb89ac3dadc125078 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 20 May 2019 00:35:36 +0200 Subject: [PATCH] test: improve unexpected warnings error If someone adds an `expectsWarning` listener without handling all warning triggered in that test file, it'll result in a cryptic error message. This improves the situation by providing an explicit error about the unexpected warning. PR-URL: https://github.com/nodejs/node/pull/28138 Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott Reviewed-By: Yongsheng Zhang Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- test/common/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/common/index.js b/test/common/index.js index 32353621662827..03d42997436772 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -528,7 +528,15 @@ let catchWarning; function expectWarning(nameOrMap, expected, code) { if (catchWarning === undefined) { catchWarning = {}; - process.on('warning', (warning) => catchWarning[warning.name](warning)); + process.on('warning', (warning) => { + if (!catchWarning[warning.name]) { + throw new TypeError( + `"${warning.name}" was triggered without being expected.\n` + + util.inspect(warning) + ); + } + catchWarning[warning.name](warning); + }); } if (typeof nameOrMap === 'string') { catchWarning[nameOrMap] = _expectWarning(nameOrMap, expected, code);