summaryrefslogtreecommitdiff
path: root/test/parallel/test-queue-microtask-uncaught-asynchooks.js
blob: ee64c6e68ab7ab6f5219c6931c5a84d6b406be12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');

// Regression test for https://github.com/nodejs/node/issues/30080:
// An uncaught exception inside a queueMicrotask callback should not lead
// to multiple after() calls for it.

let µtaskId;
const events = [];

async_hooks.createHook({
  init(id, type, triggerId, resoure) {
    if (type === 'Microtask') {
      µtaskId = id;
      events.push('init');
    }
  },
  before(id) {
    if (id === µtaskId) events.push('before');
  },
  after(id) {
    if (id === µtaskId) events.push('after');
  },
  destroy(id) {
    if (id === µtaskId) events.push('destroy');
  }
}).enable();

queueMicrotask(() => { throw new Error(); });

process.on('uncaughtException', common.mustCall());
process.on('exit', () => {
  assert.deepStrictEqual(events, ['init', 'after', 'before', 'destroy']);
});