node/test/parallel/test-promises-warning-on-unhandled-rejection.js
Dan Fabulich 3b10f7f933
process: change default --unhandled-rejections=throw
This is a semver-major change that resolves DEP0018.

All users that have set an unhandledRejection hook or set a non-default
value for the --unhandled-rejections flag will see no change in behavior
after this change.

Refs: https://nodejs.org/dist/latest/docs/api/deprecations.html#deprecations_dep0018_unhandled_promise_rejections

PR-URL: https://github.com/nodejs/node/pull/33021
Fixes: https://github.com/nodejs/node/issues/20392
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Mary Marchini <oss@mmarchini.me>
Reviewed-By: Shelley Vohr <codebytere@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
2020-09-22 12:29:32 -07:00

51 lines
1.8 KiB
JavaScript

// Flags: --no-warnings --unhandled-rejections=warn
'use strict';
// Test that warnings are emitted when a Promise experiences an uncaught
// rejection, and then again if the rejection is handled later on.
const common = require('../common');
const assert = require('assert');
let b = 0;
process.on('warning', common.mustCall((warning) => {
switch (b++) {
case 0:
// String rejection error displayed
assert.strictEqual(warning.message, 'This was rejected');
break;
case 1:
// Warning about rejection not being handled (will be next tick)
assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning');
assert(
/Unhandled promise rejection/.test(warning.message),
'Expected warning message to contain "Unhandled promise rejection" ' +
`but did not. Had "${warning.message}" instead.`
);
break;
case 2:
// Number rejection error displayed. Note it's been stringified
assert.strictEqual(warning.message, '42');
break;
case 3:
// Unhandled rejection warning (won't be handled next tick)
assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning');
assert(
/Unhandled promise rejection/.test(warning.message),
'Expected warning message to contain "Unhandled promise rejection" ' +
`but did not. Had "${warning.message}" instead.`
);
break;
case 4:
// Rejection handled asynchronously.
assert.strictEqual(warning.name, 'PromiseRejectionHandledWarning');
assert(/Promise rejection was handled asynchronously/
.test(warning.message));
}
}, 5));
const p = Promise.reject('This was rejected'); // Reject with a string
setImmediate(common.mustCall(() => p.catch(() => { })));
Promise.reject(42); // Reject with a number