|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const childProcess = require('child_process'); |
| 5 | +const os = require('os'); |
| 6 | + |
| 7 | +if (process.argv[2] === 'child') { |
| 8 | + child(process.argv[3], process.argv[4]); |
| 9 | +} else { |
| 10 | + main(); |
| 11 | +} |
| 12 | + |
| 13 | +function 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 | + |
| 35 | +function 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 | +} |
0 commit comments