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