|
| 1 | +// Loaded via AVA nodeArguments --import so it runs in each test process |
| 2 | +const isWindows = process.platform === 'win32' |
| 3 | +if (isWindows) { |
| 4 | + const pid = process.pid |
| 5 | + let installed = globalThis.__avaGlobalDiagInstalled |
| 6 | + if (!installed) { |
| 7 | + globalThis.__avaGlobalDiagInstalled = true |
| 8 | + |
| 9 | + const originalExit = process.exit.bind(process) |
| 10 | + process.exit = (code) => { |
| 11 | + // eslint-disable-next-line no-console |
| 12 | + console.error(`[ava-diag] pid=${pid} process.exit called code=${code}`) |
| 13 | + return originalExit(code) |
| 14 | + } |
| 15 | + |
| 16 | + process.on('beforeExit', (code) => { |
| 17 | + // eslint-disable-next-line no-console |
| 18 | + console.error(`[ava-diag] pid=${pid} beforeExit code=${code} exitCode=${process.exitCode}`) |
| 19 | + }) |
| 20 | + |
| 21 | + process.on('exit', (code) => { |
| 22 | + // eslint-disable-next-line no-console |
| 23 | + console.error(`[ava-diag] pid=${pid} exit code=${code} exitCode=${process.exitCode}`) |
| 24 | + }) |
| 25 | + |
| 26 | + process.on('uncaughtException', (err) => { |
| 27 | + // eslint-disable-next-line no-console |
| 28 | + console.error(`[ava-diag] pid=${pid} uncaughtException`, err?.stack || String(err)) |
| 29 | + }) |
| 30 | + |
| 31 | + process.on('unhandledRejection', (reason, promise) => { |
| 32 | + // eslint-disable-next-line no-console |
| 33 | + console.error(`[ava-diag] pid=${pid} unhandledRejection`, (reason && reason.stack) || String(reason)) |
| 34 | + }) |
| 35 | + |
| 36 | + process.on('warning', (warning) => { |
| 37 | + // eslint-disable-next-line no-console |
| 38 | + console.error(`[ava-diag] pid=${pid} warning ${warning.name}: ${warning.message}\n${warning.stack}`) |
| 39 | + }) |
| 40 | + |
| 41 | + process.on('multipleResolves', (type, promise, value) => { |
| 42 | + // eslint-disable-next-line no-console |
| 43 | + console.error(`[ava-diag] pid=${pid} multipleResolves type=${type}`) |
| 44 | + }) |
| 45 | + |
| 46 | + const getHandles = (process)._getActiveHandles?.bind(process) |
| 47 | + const getRequests = (process)._getActiveRequests?.bind(process) |
| 48 | + // Small delay to reduce noise; print snapshot near end of lifecycle |
| 49 | + setTimeout(() => { |
| 50 | + try { |
| 51 | + const handles = getHandles ? getHandles() : [] |
| 52 | + const requests = getRequests ? getRequests() : [] |
| 53 | + // eslint-disable-next-line no-console |
| 54 | + console.error( |
| 55 | + `[ava-diag] pid=${pid} snapshot handles=${handles.length} requests=${requests.length} types=${handles.map(h => h?.constructor?.name || typeof h).join(',')}` |
| 56 | + ) |
| 57 | + } catch {} |
| 58 | + }, 1000) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export {} |
| 63 | + |
0 commit comments