• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4const childProcess = require('child_process');
5const os = require('os');
6
7if (process.argv[2] === 'child') {
8  child(process.argv[3], process.argv[4]);
9} else {
10  main();
11}
12
13function child(type, valueType) {
14  const { createHook } = require('async_hooks');
15  const fs = require('fs');
16
17  createHook({
18    [type]() {
19      if (valueType === 'symbol') {
20        throw Symbol('foo');
21      }
22      // eslint-disable-next-line no-throw-literal
23      throw null;
24    }
25  }).enable();
26
27  // Trigger `promiseResolve`.
28  new Promise((resolve) => resolve())
29  // Trigger `after`/`destroy`.
30    .then(() => fs.promises.readFile(__filename, 'utf8'))
31  // Make process exit with code 0 if no error caught.
32    .then(() => process.exit(0));
33}
34
35function main() {
36  const types = [ 'init', 'before', 'after', 'destroy', 'promiseResolve' ];
37  const valueTypes = [
38    [ 'null', 'Error: null' ],
39    [ 'symbol', 'Error: Symbol(foo)' ],
40  ];
41  for (const type of types) {
42    for (const [valueType, expect] of valueTypes) {
43      const cp = childProcess.spawnSync(
44        process.execPath,
45        [ __filename, 'child', type, valueType ],
46        {
47          encoding: 'utf8',
48        });
49      assert.strictEqual(cp.status, 1, type);
50      assert.strictEqual(cp.stderr.trim().split(os.EOL)[0], expect, type);
51    }
52  }
53}
54