Skip to content

Commit 450e1da

Browse files
committed
test(windows): preload AVA global diagnostics via --import to capture exit/rejections in all workers
1 parent 5e6e946 commit 450e1da

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

test/_ava-global-diagnostics.mjs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+

test/ava.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default {
33
extensions: {
44
ts: "module",
55
},
6-
nodeArguments: ["--import=tsx"],
6+
nodeArguments: ["--import=tsx", "--import=./test/_ava-global-diagnostics.mjs"],
77
environmentVariables: {
88
KIT_TEST: "true",
99
},

0 commit comments

Comments
 (0)