From a8ce95ece51552c0d29b1e6ba449bf8d479c29d8 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sun, 10 May 2026 00:28:43 +0000 Subject: [PATCH] [Tests] Replace real-time setTimeout with fake timers in plugin-cloudflare Refactored `packages/plugin-cloudflare/src/tunnel.test.ts` to use Vitest's fake timer API instead of a manual 250ms sleep. This improves test speed and reliability by making the timeout behavior deterministic. Added proper cleanup with `try...finally` to ensure real timers are restored even on failure. --- packages/plugin-cloudflare/src/tunnel.test.ts | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/packages/plugin-cloudflare/src/tunnel.test.ts b/packages/plugin-cloudflare/src/tunnel.test.ts index 73b46304406..57baee395e4 100644 --- a/packages/plugin-cloudflare/src/tunnel.test.ts +++ b/packages/plugin-cloudflare/src/tunnel.test.ts @@ -119,26 +119,31 @@ describe('hookStart', () => { test('cleans errors coming from the log', async () => { // Given - vi.mocked(exec).mockImplementation(async (command, args, options) => { - const writable = options?.stdout as Writable - writable.write( - Buffer.from( - `2023-10-11T13:32:45Z ERR Failed to serve quic connection error="Application error 0x0 (remote)" connIndex=0 event=0 ip=123.123.123.123`, - ), - ) - }) - // When - const tunnelClient = (await hookStart(port)).valueOrAbort() - await new Promise((resolve) => setTimeout(resolve, 250)) - const result = tunnelClient.getTunnelStatus() - - // Then - expect(result).toEqual({ - status: 'error', - message: - 'Could not start Cloudflare tunnel: Failed to serve quic connection error="Application error 0x0 (remote)" ', - tryMessage: expect.anything(), - }) + vi.useFakeTimers() + try { + vi.mocked(exec).mockImplementation(async (command, args, options) => { + const writable = options?.stdout as Writable + writable.write( + Buffer.from( + `2023-10-11T13:32:45Z ERR Failed to serve quic connection error="Application error 0x0 (remote)" connIndex=0 event=0 ip=123.123.123.123`, + ), + ) + }) + // When + const tunnelClient = (await hookStart(port)).valueOrAbort() + await vi.advanceTimersByTimeAsync(250) + const result = tunnelClient.getTunnelStatus() + + // Then + expect(result).toEqual({ + status: 'error', + message: + 'Could not start Cloudflare tunnel: Failed to serve quic connection error="Application error 0x0 (remote)" ', + tryMessage: expect.anything(), + }) + } finally { + vi.useRealTimers() + } }) test('returns error if it fails to install cloudflared', async () => {