11cb0ef41Sopenharmony_ci// Flags: --no-warnings --unhandled-rejections=warn
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Test that warnings are emitted when a Promise experiences an uncaught
51cb0ef41Sopenharmony_ci// rejection, and then again if the rejection is handled later on.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cilet b = 0;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciprocess.on('warning', common.mustCall((warning) => {
131cb0ef41Sopenharmony_ci  switch (b++) {
141cb0ef41Sopenharmony_ci    case 0:
151cb0ef41Sopenharmony_ci      // String rejection error displayed
161cb0ef41Sopenharmony_ci      assert.strictEqual(warning.message, 'This was rejected');
171cb0ef41Sopenharmony_ci      break;
181cb0ef41Sopenharmony_ci    case 1:
191cb0ef41Sopenharmony_ci      // Warning about rejection not being handled (will be next tick)
201cb0ef41Sopenharmony_ci      assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning');
211cb0ef41Sopenharmony_ci      assert(
221cb0ef41Sopenharmony_ci        /Unhandled promise rejection/.test(warning.message),
231cb0ef41Sopenharmony_ci        'Expected warning message to contain "Unhandled promise rejection" ' +
241cb0ef41Sopenharmony_ci        `but did not. Had "${warning.message}" instead.`
251cb0ef41Sopenharmony_ci      );
261cb0ef41Sopenharmony_ci      break;
271cb0ef41Sopenharmony_ci    case 2:
281cb0ef41Sopenharmony_ci      // Number rejection error displayed. Note it's been stringified
291cb0ef41Sopenharmony_ci      assert.strictEqual(warning.message, '42');
301cb0ef41Sopenharmony_ci      break;
311cb0ef41Sopenharmony_ci    case 3:
321cb0ef41Sopenharmony_ci      // Unhandled rejection warning (won't be handled next tick)
331cb0ef41Sopenharmony_ci      assert.strictEqual(warning.name, 'UnhandledPromiseRejectionWarning');
341cb0ef41Sopenharmony_ci      assert(
351cb0ef41Sopenharmony_ci        /Unhandled promise rejection/.test(warning.message),
361cb0ef41Sopenharmony_ci        'Expected warning message to contain "Unhandled promise rejection" ' +
371cb0ef41Sopenharmony_ci        `but did not. Had "${warning.message}" instead.`
381cb0ef41Sopenharmony_ci      );
391cb0ef41Sopenharmony_ci      break;
401cb0ef41Sopenharmony_ci    case 4:
411cb0ef41Sopenharmony_ci      // Rejection handled asynchronously.
421cb0ef41Sopenharmony_ci      assert.strictEqual(warning.name, 'PromiseRejectionHandledWarning');
431cb0ef41Sopenharmony_ci      assert(/Promise rejection was handled asynchronously/
441cb0ef41Sopenharmony_ci        .test(warning.message));
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci}, 5));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciconst p = Promise.reject('This was rejected'); // Reject with a string
491cb0ef41Sopenharmony_cisetImmediate(common.mustCall(() => p.catch(() => { })));
501cb0ef41Sopenharmony_ciPromise.reject(42); // Reject with a number
51