mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
df08779e0d
This commit removes `common.crashOnUnhandledRejection()` and adds `common.disableCrashOnUnhandledRejection()`. To reduce the risk of mistakes and make writing tests that involve promises simpler, always install the unhandledRejection hook in tests and provide a way to disable it for the rare cases where it's needed. PR-URL: https://github.com/nodejs/node/pull/21849 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
57 lines
2.0 KiB
JavaScript
57 lines
2.0 KiB
JavaScript
// Flags: --no-warnings
|
|
'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');
|
|
|
|
common.disableCrashOnUnhandledRejection();
|
|
|
|
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:
|
|
// One time deprecation warning, first unhandled rejection
|
|
assert.strictEqual(warning.name, 'DeprecationWarning');
|
|
break;
|
|
case 3:
|
|
// Number rejection error displayed. Note it's been stringified
|
|
assert.strictEqual(warning.message, '42');
|
|
break;
|
|
case 4:
|
|
// 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 5:
|
|
// Rejection handled asynchronously.
|
|
assert.strictEqual(warning.name, 'PromiseRejectionHandledWarning');
|
|
assert(/Promise rejection was handled asynchronously/
|
|
.test(warning.message));
|
|
}
|
|
}, 6));
|
|
|
|
const p = Promise.reject('This was rejected'); // Reject with a string
|
|
setImmediate(common.mustCall(() => p.catch(() => { })));
|
|
Promise.reject(42); // Reject with a number
|