1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const { Worker } = require('worker_threads'); 5 6// Do not use isMainThread so that this test itself can be run inside a Worker. 7if (!process.env.HAS_STARTED_WORKER) { 8 process.env.HAS_STARTED_WORKER = 1; 9 const w = new Worker(__filename); 10 w.on('message', common.mustNotCall()); 11 w.on('error', common.mustCall((err) => { 12 console.log(err.message); 13 assert(/^Error: foo$/.test(err)); 14 })); 15 w.on('exit', common.mustCall((code) => { 16 // uncaughtException is code 1 17 assert.strictEqual(code, 1); 18 })); 19} else { 20 // Cannot use common.mustCall as it cannot catch this 21 let called = false; 22 process.on('exit', (code) => { 23 if (!called) { 24 called = true; 25 } else { 26 assert.fail('Exit callback called twice in worker'); 27 } 28 }); 29 30 setTimeout(() => assert.fail('Timeout executed after uncaughtException'), 31 2000); 32 33 throw new Error('foo'); 34} 35