1// Flags: --no-warnings 2'use strict'; 3 4// Test that warnings are emitted when a Promise experiences an uncaught 5// rejection, and then again if the rejection is handled later on. 6 7const common = require('../common'); 8const assert = require('assert'); 9 10common.disableCrashOnUnhandledRejection(); 11 12let b = 0; 13 14process.on('warning', common.mustCall((warning) => { 15 switch (b++) { 16 case 0: 17 // String rejection error displayed 18 assert.strictEqual(warning.message, 'This was rejected'); 19 break; 20 case 1: 21 // Warning about rejection not being handled (will be next tick) 22 assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning'); 23 assert( 24 /Unhandled promise rejection/.test(warning.message), 25 'Expected warning message to contain "Unhandled promise rejection" ' + 26 `but did not. Had "${warning.message}" instead.` 27 ); 28 break; 29 case 2: 30 // One time deprecation warning, first unhandled rejection 31 assert.strictEqual(warning.name, 'DeprecationWarning'); 32 break; 33 case 3: 34 // Number rejection error displayed. Note it's been stringified 35 assert.strictEqual(warning.message, '42'); 36 break; 37 case 4: 38 // Unhandled rejection warning (won't be handled next tick) 39 assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning'); 40 assert( 41 /Unhandled promise rejection/.test(warning.message), 42 'Expected warning message to contain "Unhandled promise rejection" ' + 43 `but did not. Had "${warning.message}" instead.` 44 ); 45 break; 46 case 5: 47 // Rejection handled asynchronously. 48 assert.strictEqual(warning.name, 'PromiseRejectionHandledWarning'); 49 assert(/Promise rejection was handled asynchronously/ 50 .test(warning.message)); 51 } 52}, 6)); 53 54const p = Promise.reject('This was rejected'); // Reject with a string 55setImmediate(common.mustCall(() => p.catch(() => { }))); 56Promise.reject(42); // Reject with a number 57