From 86bf64a2540f23fcb1d439db4faeaf3fcea0d117 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. --- 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);