1// Flags: --unhandled-rejections=throw
2'use strict';
3
4const common = require('../common');
5const Countdown = require('../common/countdown');
6const assert = require('assert');
7
8// Verify that the unhandledRejection handler prevents triggering
9// uncaught exceptions
10
11const err1 = new Error('One');
12
13const errors = [err1, null];
14
15const ref = new Promise(() => {
16  throw err1;
17});
18// Explicitly reject `null`.
19Promise.reject(null);
20
21process.on('warning', common.mustNotCall('warning'));
22process.on('rejectionHandled', common.mustNotCall('rejectionHandled'));
23process.on('exit', assert.strictEqual.bind(null, 0));
24process.on('uncaughtException', common.mustNotCall('uncaughtException'));
25
26const timer = setTimeout(() => console.log(ref), 1000);
27
28const counter = new Countdown(2, () => {
29  clearTimeout(timer);
30});
31
32process.on('unhandledRejection', common.mustCall((err) => {
33  counter.dec();
34  const knownError = errors.shift();
35  assert.deepStrictEqual(err, knownError);
36}, 2));
37