Skip to content

Commit

Permalink
test: improve unexpected warnings error
Browse files Browse the repository at this point in the history
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: nodejs#28138
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and Trott committed Jun 13, 2019
1 parent c1f0cbe commit 18a8eec
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 18a8eec

Please sign in to comment.