1// Flags: --unhandled-rejections=strict 2'use strict'; 3 4const common = require('../common'); 5const Countdown = require('../common/countdown'); 6const assert = require('assert'); 7 8// Verify that unhandled rejections always trigger uncaught exceptions instead 9// of triggering unhandled rejections. 10 11const err1 = new Error('One'); 12const err2 = new Error( 13 'This error originated either by throwing ' + 14 'inside of an async function without a catch block, or by rejecting a ' + 15 'promise which was not handled with .catch(). The promise rejected with the' + 16 ' reason "null".' 17); 18err2.code = 'ERR_UNHANDLED_REJECTION'; 19Object.defineProperty(err2, 'name', { 20 value: 'UnhandledPromiseRejection', 21 writable: true, 22 configurable: true 23}); 24 25const errors = [err1, err2]; 26const identical = [true, false]; 27 28const ref = new Promise(() => { 29 throw err1; 30}); 31// Explicitly reject `null`. 32Promise.reject(null); 33 34process.on('warning', common.mustNotCall('warning')); 35process.on('unhandledRejection', common.mustCall(2)); 36process.on('rejectionHandled', common.mustNotCall('rejectionHandled')); 37process.on('exit', assert.strictEqual.bind(null, 0)); 38 39const timer = setTimeout(() => console.log(ref), 1000); 40 41const counter = new Countdown(2, () => { 42 clearTimeout(timer); 43}); 44 45process.on('uncaughtException', common.mustCall((err, origin) => { 46 counter.dec(); 47 assert.strictEqual(origin, 'unhandledRejection', err); 48 const knownError = errors.shift(); 49 assert.deepStrictEqual(err, knownError); 50 // Check if the errors are reference equal. 51 assert(identical.shift() ? err === knownError : err !== knownError); 52}, 2)); 53