fix(tests): prevent telemetry singleton from polluting parallel test 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>
This commit is contained in:
B 2026-04-30 18:47:03 +00:00
parent 844808cd7d
commit 6818428172
2 changed files with 9 additions and 0 deletions

View file

@ -60,6 +60,10 @@ cleanupStrayTestFiles();
const TEST_HOME = mkdtempSync(join(tmpdir(), "spawn-test-home-"));
// Disable telemetry in tests to prevent fire-and-forget fetch calls from
// interfering with other test files' global.fetch mocks.
process.env.SPAWN_TELEMETRY = "0";
// Redirect all user-directory env vars to the isolated temp
process.env.HOME = TEST_HOME;
process.env.XDG_CACHE_HOME = join(TEST_HOME, ".cache");

View file

@ -242,6 +242,11 @@ export function captureError(type: string, err: unknown): void {
/** Send a single event to PostHog immediately. Fire-and-forget. */
function sendEvent(event: string, properties: Record<string, unknown>): void {
// Re-check at send time — guards against singleton state leaking in tests
// where initTelemetry() was called with _enabled=true but env was later restored.
if (process.env.BUN_ENV === "test" || process.env.NODE_ENV === "test" || process.env.SPAWN_TELEMETRY === "0") {
return;
}
const body = JSON.stringify({
api_key: POSTHOG_TOKEN,
batch: [