Skip to content

fix(tests): prevent telemetry singleton from polluting parallel test fetch mocks#3376

Open
la14-1 wants to merge 1 commit intomainfrom
fix/flaky-test-isolation
Open

fix(tests): prevent telemetry singleton from polluting parallel test fetch mocks#3376
la14-1 wants to merge 1 commit intomainfrom
fix/flaky-test-isolation

Conversation

@la14-1
Copy link
Copy Markdown
Member

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

Why: Two tests (hetzner-cov resource_limit retry and digitalocean-token OAuth recovery) consistently fail in the full suite because the telemetry singleton's _enabled flag leaks across parallel test files. When telemetry.test.ts enables telemetry, logWarn calls in other tests trigger fire-and-forget fetch() calls that increment callCount-based mock assertions.

Root cause

telemetry.test.ts deletes BUN_ENV and NODE_ENV to test telemetry in "production" mode, then calls initTelemetry() which sets _enabled = true. Since bun runs test files in the same process with shared module singletons, _enabled stays true for all concurrent test files. Any logWarn/logError call then fires sendEventfetch() through other tests' global.fetch mocks.

Fix

  1. Runtime guard in sendEvent() — checks BUN_ENV, NODE_ENV, and SPAWN_TELEMETRY at call time, not just at init
  2. SPAWN_TELEMETRY=0 in test preload — defense-in-depth for all tests

Testing

  • Full suite: 2138 pass, 0 fail (was 2136 pass, 2 fail)
  • bun test src/__tests__/hetzner-cov.test.ts src/__tests__/telemetry.test.ts — passes
  • bun test src/__tests__/digitalocean-token.test.ts src/__tests__/telemetry.test.ts — passes
  • Telemetry tests themselves: 19 pass, 0 fail

-- refactor/code-health

@la14-1
Copy link
Copy Markdown
Member Author

la14-1 commented May 1, 2026

Dedup analysis: This is the most comprehensive fix for the telemetry fetch leak affecting #3353, #3358, and #3365. All four PRs address the same root issue (telemetry singleton _enabled leaking across parallel test files).

Recommendation: #3376 supersedes #3358 (both modify telemetry.ts). #3365 is complementary but may be unnecessary once this lands. #3353 is already noted as superseded.

-- refactor/pr-maintainer

…fetch mocks

The telemetry module's `_enabled` flag persists across parallel test files
when `telemetry.test.ts` calls `initTelemetry()` (which deletes BUN_ENV/NODE_ENV
guards). This causes `logWarn` → `captureWarning` → `sendEvent` → `fetch()` to
fire unexpected calls through other tests' `global.fetch` mocks, breaking
callCount-based assertions in `hetzner-cov.test.ts` and `digitalocean-token.test.ts`.

Fix:
- Add runtime env guard in `sendEvent()` so telemetry never fires in test env
- Set `SPAWN_TELEMETRY=0` in test preload as defense-in-depth

Agent: code-health
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@la14-1 la14-1 force-pushed the fix/flaky-test-isolation branch from f815f3e to cf61613 Compare May 2, 2026 10:19
@la14-1
Copy link
Copy Markdown
Member Author

la14-1 commented May 2, 2026

Rebased onto main (resolved package.json version conflict — kept v1.0.36 from main). Branch is now clean with 1 commit on top of current main.

-- refactor/pr-maintainer

@la14-1
Copy link
Copy Markdown
Member Author

la14-1 commented May 4, 2026

Verified in worktree: all 2238 tests pass (0 failures — this PR fixes both pre-existing flaky test failures on main), lint clean (biome 0 errors). PR is mergeable and ready for review. Note: this PR should be merged before other PRs to fix the test suite.

-- refactor/pr-maintainer

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