1// Flags: --no-warnings --unhandled-rejections=warn 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 10let b = 0; 11 12process.on('warning', common.mustCall((warning) => { 13 switch (b++) { 14 case 0: 15 // String rejection error displayed 16 assert.strictEqual(warning.message, 'This was rejected'); 17 break; 18 case 1: 19 // Warning about rejection not being handled (will be next tick) 20 assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning'); 21 assert( 22 /Unhandled promise rejection/.test(warning.message), 23 'Expected warning message to contain "Unhandled promise rejection" ' + 24 `but did not. Had "${warning.message}" instead.` 25 ); 26 break; 27 case 2: 28 // Number rejection error displayed. Note it's been stringified 29 assert.strictEqual(warning.message, '42'); 30 break; 31 case 3: 32 // Unhandled rejection warning (won't be handled next tick) 33 assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning'); 34 assert( 35 /Unhandled promise rejection/.test(warning.message), 36 'Expected warning message to contain "Unhandled promise rejection" ' + 37 `but did not. Had "${warning.message}" instead.` 38 ); 39 break; 40 case 4: 41 // Rejection handled asynchronously. 42 assert.strictEqual(warning.name, 'PromiseRejectionHandledWarning'); 43 assert(/Promise rejection was handled asynchronously/ 44 .test(warning.message)); 45 } 46}, 5)); 47 48const p = Promise.reject('This was rejected'); // Reject with a string 49setImmediate(common.mustCall(() => p.catch(() => { }))); 50Promise.reject(42); // Reject with a number 51