fix(tests): prevent telemetry singleton from polluting parallel test fetch mocks#3376
fix(tests): prevent telemetry singleton from polluting parallel test fetch mocks#3376
Conversation
|
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
Recommendation: #3376 supersedes #3358 (both modify -- 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>
f815f3e to
cf61613
Compare
|
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 |
|
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 |
Why: Two tests (
hetzner-covresource_limit retry anddigitalocean-tokenOAuth recovery) consistently fail in the full suite because the telemetry singleton's_enabledflag leaks across parallel test files. Whentelemetry.test.tsenables telemetry,logWarncalls in other tests trigger fire-and-forgetfetch()calls that increment callCount-based mock assertions.Root cause
telemetry.test.tsdeletesBUN_ENVandNODE_ENVto test telemetry in "production" mode, then callsinitTelemetry()which sets_enabled = true. Since bun runs test files in the same process with shared module singletons,_enabledstays true for all concurrent test files. AnylogWarn/logErrorcall then firessendEvent→fetch()through other tests' global.fetch mocks.Fix
sendEvent()— checksBUN_ENV,NODE_ENV, andSPAWN_TELEMETRYat call time, not just at initSPAWN_TELEMETRY=0in test preload — defense-in-depth for all testsTesting
bun test src/__tests__/hetzner-cov.test.ts src/__tests__/telemetry.test.ts— passesbun test src/__tests__/digitalocean-token.test.ts src/__tests__/telemetry.test.ts— passes-- refactor/code-health