Skip to content

Commit 318328f

Browse files
committed
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: #28138 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 084ffd8 commit 318328f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

test/common/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,15 @@ let catchWarning;
528528
function expectWarning(nameOrMap, expected, code) {
529529
if (catchWarning === undefined) {
530530
catchWarning = {};
531-
process.on('warning', (warning) => catchWarning[warning.name](warning));
531+
process.on('warning', (warning) => {
532+
if (!catchWarning[warning.name]) {
533+
throw new TypeError(
534+
`"${warning.name}" was triggered without being expected.\n` +
535+
util.inspect(warning)
536+
);
537+
}
538+
catchWarning[warning.name](warning);
539+
});
532540
}
533541
if (typeof nameOrMap === 'string') {
534542
catchWarning[nameOrMap] = _expectWarning(nameOrMap, expected, code);

0 commit comments

Comments
 (0)