Skip to content

fix(tests): disable telemetry in afterEach to prevent cross-file fetch leak#3358

Closed
la14-1 wants to merge 1 commit intomainfrom
fix-telemetry-test-leak
Closed

fix(tests): disable telemetry in afterEach to prevent cross-file fetch leak#3358
la14-1 wants to merge 1 commit intomainfrom
fix-telemetry-test-leak

Conversation

@la14-1
Copy link
Copy Markdown
Member

@la14-1 la14-1 commented Apr 25, 2026

Why: 2 tests fail on every bun test run (hetzner-cov, digitalocean-token) because the telemetry test enables _enabled = true globally and never resets it — every subsequent logWarn/logError triggers fire-and-forget fetch() calls that corrupt other tests' callCount-based mocks.

Root cause

telemetry.test.ts deletes BUN_ENV/NODE_ENV and calls initTelemetry() to enable telemetry for testing. Once _enabled is true, it stays true for the entire process. Any logWarn() or logError() in concurrent test files (hetzner, digitalocean) fires sendEvent()fetch() through the calling test's mock, consuming callCounts.

Fix (2 lines of real change)

  1. Export _testHelpers.enabled getter/setter from telemetry.ts
  2. Set telemetryTestHelpers.enabled = false in telemetry.test.ts afterEach

This fixes the leak at the source rather than patching each affected test to filter PostHog URLs.

Relationship to #3341 and #3353

Both existing PRs work around the symptom (patching individual test mocks to ignore telemetry URLs). This PR fixes the root cause — once merged, those workarounds become unnecessary and no future test can be affected by telemetry fetch leaks.

Test plan

  • bun test passes with 0 failures (2108 pass, 5376 expect() calls)
  • Verified on 2 consecutive runs — no flakiness
  • biome check passes on changed files

-- spawn-refactor/test-engineer

@la14-1 la14-1 marked this pull request as ready for review April 25, 2026 03:15
@la14-1 la14-1 force-pushed the fix-telemetry-test-leak branch from 96a40ca to 874fd8a Compare April 26, 2026 05:04
@la14-1
Copy link
Copy Markdown
Member Author

la14-1 commented Apr 28, 2026

Overlap note: This PR fixes the root cause (telemetry _enabled leaking across test files) of the same 2 flaky tests addressed by #3353 and #3365.

Recommended merge order: this PR first (root cause), then optionally #3365 (defense in depth). #3353 can likely be closed if both land.

All CI checks passing. No merge conflicts.

-- refactor/pr-maintainer

@la14-1
Copy link
Copy Markdown
Member Author

la14-1 commented Apr 28, 2026

This PR has been open for 3+ days with all CI checks passing (ShellCheck, Biome Lint, Unit Tests, Mock Tests, macOS Compatibility — all SUCCESS). It is mergeable with no conflicts.

This is the root-cause fix for the flaky test issue (telemetry leak). Recommended merge order: this PR first, then #3365 (defense in depth). #3353 can be closed after both land.

Requesting human review when convenient.

-- spawn-refactor/pr-maintainer

…test files

The telemetry test enables telemetry globally by deleting BUN_ENV/NODE_ENV
and calling initTelemetry(). Once enabled, every logWarn/logError in the
process fires sendEvent() → fetch(), which consumes mock callCounts in
concurrent test files (hetzner-cov, digitalocean-token), causing 2
consistent test failures.

Add _testHelpers.enabled to telemetry.ts and reset it in afterEach so
telemetry is disabled before other test files' mocks can be affected.

Agent: test-engineer
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@la14-1
Copy link
Copy Markdown
Member Author

la14-1 commented May 1, 2026

Dedup update: #3376 (opened Apr 30) supersedes this PR — it also modifies telemetry.ts with a root-cause fix (runtime env-var guard in sendEvent()) plus adds SPAWN_TELEMETRY=0 in preload.ts for defense-in-depth. Both PRs fix the same leak but #3376 is more comprehensive and will conflict with this one on telemetry.ts.

-- refactor/pr-maintainer

@la14-1
Copy link
Copy Markdown
Member Author

la14-1 commented May 1, 2026

Closing as superseded by #3376, which provides a more comprehensive fix for the same telemetry singleton leak (runtime env-var guard in sendEvent() + SPAWN_TELEMETRY=0 in test preload).

-- refactor/pr-maintainer

@la14-1
Copy link
Copy Markdown
Member Author

la14-1 commented May 1, 2026

Superseded by #3376.

@la14-1 la14-1 closed this May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants