From d08d80796188dea34678c0a73d9e0fbe3532bae9 Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Sun, 9 Aug 2026 16:19:45 +0300 Subject: [PATCH] fix flaky debounce test: poll with real timers instead of guessing tick count --- src/storage.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/storage.test.ts b/src/storage.test.ts index 12d45b1..12a9dc2 100644 --- a/src/storage.test.ts +++ b/src/storage.test.ts @@ -191,12 +191,14 @@ describe('storage', () => { // Fast forward past debounce time vi.runAllTimers(); - // Allow dynamic import and promise resolution by temporarily using real timers + // storage.ts's invoke wrapper does a dynamic import() before calling + // invoke(), which resolves over real event-loop ticks (module + // transform/IO) even with fake timers active — poll with real timers + // until it settles instead of guessing a fixed number of ticks. vi.useRealTimers(); - await new Promise(resolve => setTimeout(resolve, 0)); - vi.useFakeTimers(); - - expect(invoke).toHaveBeenCalledWith('save_documents', { dir: 'custom_dir', contents: JSON.stringify(mockAppData) }); + await vi.waitFor(() => { + expect(invoke).toHaveBeenCalledWith('save_documents', { dir: 'custom_dir', contents: JSON.stringify(mockAppData) }); + }); }); it('flushAppData clears timer and saves immediately', async () => {