fix(test): clear boundedPromise timers to prevent unhandled rejections (#4220)

boundedPromise timeouts could fire after test completion,
causing vitest to exit with code 1 due to unhandled
rejection. Add clear() method and cleanup all pending
timers in the finally block.
This commit is contained in:
Shaojin Wen 2026-05-17 08:28:22 +08:00 committed by GitHub
parent 07165a095c
commit 5ce2f2854b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -328,6 +328,7 @@ describe('AbortController and Process Lifecycle (E2E)', () => {
const promise = new Promise<void>((resolve, reject) => {
resolveFn = () => {
if (timer !== undefined) clearTimeout(timer);
timer = undefined;
resolve();
};
pendingReject = reject;
@ -335,10 +336,17 @@ describe('AbortController and Process Lifecycle (E2E)', () => {
const startTimer = () => {
if (timer !== undefined) return;
timer = setTimeout(() => {
timer = undefined;
pendingReject(new Error(`${label} timeout after ${ms}ms`));
}, ms);
};
return { promise, resolve: () => resolveFn(), startTimer };
const clear = () => {
if (timer !== undefined) {
clearTimeout(timer);
timer = undefined;
}
};
return { promise, resolve: () => resolveFn(), startTimer, clear };
};
const canUseToolCalled = boundedPromise(
@ -348,6 +356,12 @@ describe('AbortController and Process Lifecycle (E2E)', () => {
const inputStreamDone = boundedPromise('inputStreamDone', 15000);
const firstResult = boundedPromise('firstResult', 30000);
const secondResult = boundedPromise('secondResult', 30000);
const pendingTimers = [
canUseToolCalled,
inputStreamDone,
firstResult,
secondResult,
];
// firstResult begins as soon as the query starts.
firstResult.startTimer();
@ -454,6 +468,7 @@ describe('AbortController and Process Lifecycle (E2E)', () => {
const content = await helper.readFile('test.txt');
expect(content).toBe('original content');
} finally {
for (const t of pendingTimers) t.clear();
await q.close();
}
});