fix(tests): prevent telemetry module from leaking fetch calls across 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>
This commit is contained in:
B 2026-04-25 03:13:22 +00:00
parent 83ecccf4ad
commit e7861eb83d
2 changed files with 17 additions and 0 deletions

View file

@ -13,6 +13,7 @@
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
import { isString } from "@openrouter/spawn-shared";
import * as v from "valibot";
import { _testHelpers as telemetryTestHelpers } from "../shared/telemetry.js";
// ── Schemas for validating PostHog payloads ─────────────────────────────────
@ -127,6 +128,10 @@ describe("telemetry", () => {
afterEach(() => {
global.fetch = originalFetch;
// Disable telemetry so fire-and-forget fetch calls don't leak into other
// test files running in the same process (the root cause of flaky
// hetzner-cov and digitalocean-token test failures).
telemetryTestHelpers.enabled = false;
if (originalTelemetry !== undefined) {
process.env.SPAWN_TELEMETRY = originalTelemetry;
} else {

View file

@ -270,3 +270,15 @@ function sendEvent(event: string, properties: Record<string, unknown>): void {
}),
);
}
// ── Test Helpers ──────────────────────────────────────────────────────────────
/** Exposed for test cleanup — prevents telemetry from leaking across test files. */
export const _testHelpers = {
get enabled(): boolean {
return _enabled;
},
set enabled(val: boolean) {
_enabled = val;
},
};