From 3884e1a884c3bb7bf2ed4190137b0dc759c73ffa Mon Sep 17 00:00:00 2001 From: B <6723574+louisgv@users.noreply.github.com> Date: Fri, 24 Apr 2026 14:48:34 +0000 Subject: [PATCH] fix(tests): filter PostHog telemetry from fetch mocks to fix flaky tests Two tests (hetzner-cov and digitalocean-token) failed consistently in the full suite because telemetry.test.ts enables PostHog telemetry via the shared module singleton, and those async fetch calls leak into other test files' globalThis.fetch mocks, incrementing callCount and shifting mock response order. Fix: filter non-test URLs in affected mocks so telemetry calls get a benign response without affecting callCount. Agent: test-engineer Co-Authored-By: Claude Sonnet 4.5 --- .../cli/src/__tests__/digitalocean-token.test.ts | 13 +++++++++++-- packages/cli/src/__tests__/hetzner-cov.test.ts | 7 ++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/__tests__/digitalocean-token.test.ts b/packages/cli/src/__tests__/digitalocean-token.test.ts index e0d329129..2aef35680 100644 --- a/packages/cli/src/__tests__/digitalocean-token.test.ts +++ b/packages/cli/src/__tests__/digitalocean-token.test.ts @@ -90,8 +90,12 @@ describe("doApi 401 OAuth recovery", () => { state.token = "expired-token"; let callCount = 0; globalThis.fetch = mock((url: string | URL | Request) => { - callCount++; const urlStr = String(url); + // Ignore background telemetry calls (PostHog) that leak from other test files + if (!urlStr.includes("digitalocean")) { + return Promise.resolve(new Response("ok")); + } + callCount++; // First call: the actual API call returning 401 if (callCount === 1) { return Promise.resolve( @@ -147,7 +151,12 @@ describe("doApi 401 OAuth recovery", () => { state.token = "expired-token"; _testHelpers.recovering401 = true; let callCount = 0; - globalThis.fetch = mock(() => { + globalThis.fetch = mock((url: string | URL | Request) => { + const urlStr = String(url); + // Ignore background telemetry calls (PostHog) that leak from other test files + if (!urlStr.includes("digitalocean")) { + return Promise.resolve(new Response("ok")); + } callCount++; return Promise.resolve( new Response("Unauthorized", { diff --git a/packages/cli/src/__tests__/hetzner-cov.test.ts b/packages/cli/src/__tests__/hetzner-cov.test.ts index ed1c050f1..78bb88d5f 100644 --- a/packages/cli/src/__tests__/hetzner-cov.test.ts +++ b/packages/cli/src/__tests__/hetzner-cov.test.ts @@ -586,7 +586,12 @@ describe("hetzner/createServer", () => { }, }; let callCount = 0; - global.fetch = mock(() => { + global.fetch = mock((url: string | URL | Request) => { + const urlStr = String(url instanceof Request ? url.url : url); + // Ignore background telemetry calls (PostHog) that leak from other test files + if (!urlStr.includes("hetzner.cloud")) { + return Promise.resolve(new Response("ok")); + } callCount++; if (callCount <= 1) { // Token validation