Skip to content

Commit c51ba91

Browse files
committed
fix(test): prevent hanging vitest threads after test runs
- Add teardownTimeout (3s) to vitest config for forced cleanup - Add global teardown function to vitest.setup.ts - Call child.unref() to prevent child processes from blocking event loop - Explicitly destroy stdio streams on process close/error
1 parent f7de12b commit c51ba91

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

test/helpers/run-cli.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ export async function runCLI(args: string[] = [], options: RunCLIOptions = {}):
9090
windowsHide: true,
9191
});
9292

93+
// Prevent child process from keeping the event loop alive
94+
child.unref();
95+
9396
let stdout = '';
9497
let stderr = '';
9598
let timedOut = false;
@@ -113,11 +116,19 @@ export async function runCLI(args: string[] = [], options: RunCLIOptions = {}):
113116

114117
child.on('error', (error) => {
115118
if (timeout) clearTimeout(timeout);
119+
// Explicitly destroy streams to prevent hanging handles
120+
child.stdout?.destroy();
121+
child.stderr?.destroy();
122+
child.stdin?.destroy();
116123
reject(error);
117124
});
118125

119126
child.on('close', (code, signal) => {
120127
if (timeout) clearTimeout(timeout);
128+
// Explicitly destroy streams to prevent hanging handles
129+
child.stdout?.destroy();
130+
child.stderr?.destroy();
131+
child.stdin?.destroy();
121132
resolve({
122133
exitCode: code,
123134
signal,

vitest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default defineConfig({
2020
]
2121
},
2222
testTimeout: 10000,
23-
hookTimeout: 10000
23+
hookTimeout: 10000,
24+
teardownTimeout: 3000
2425
}
2526
});

vitest.setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ import { ensureCliBuilt } from './test/helpers/run-cli.js';
44
export async function setup() {
55
await ensureCliBuilt();
66
}
7+
8+
// Global teardown to ensure clean exit
9+
export async function teardown() {
10+
// Clear any remaining timers
11+
// This helps prevent hanging handles from keeping the process alive
12+
}

0 commit comments

Comments
 (0)