Skip to content

The platform for continuously improving AI agents.

Disable PostHog while Vitest runs (#345)

Pritam Sonicd91d9dc1b8d
diff --git a/frontend/src/analytics.ts b/frontend/src/analytics.ts
index 00ad5cbd..fbc3be7c 100644
--- a/frontend/src/analytics.ts
+++ b/frontend/src/analytics.ts
@@ -1,5 +1,7 @@
const selfHosted = import.meta.env.VITE_SELF_HOSTED === "true";
-const enabled = !selfHosted && import.meta.env.MODE !== "development";
+// Vitest's mode is "test", so "not development" would still init the real project.
+const enabled =
+ !selfHosted && import.meta.env.MODE !== "development" && import.meta.env.MODE !== "test";
const client = enabled
? import("posthog-js")
diff --git a/frontend/src/self-hosted-analytics.test.ts b/frontend/src/self-hosted-analytics.test.ts
index ed0fa6bc..39fcc594 100644
--- a/frontend/src/self-hosted-analytics.test.ts
+++ b/frontend/src/self-hosted-analytics.test.ts
@@ -34,6 +34,16 @@ describe("self-hosted PostHog strip", () => {
expect(posthogInit).not.toHaveBeenCalled();
});
+ it("skips analytics.ts init when vitest sets MODE to test", async () => {
+ vi.stubEnv("VITE_SELF_HOSTED", "false");
+ vi.stubEnv("MODE", "test");
+ const { trackEvent } = await import("./analytics");
+ trackEvent("setup_prompt_copied", { project_id: "project-1" });
+ await vi.dynamicImportSettled();
+ expect(posthogInit).not.toHaveBeenCalled();
+ expect(posthogCapture).not.toHaveBeenCalled();
+ });
+
it("initializes once and preserves events captured while the SDK loads", async () => {
vi.stubEnv("VITE_SELF_HOSTED", "false");
vi.stubEnv("VITE_PUBLIC_POSTHOG_KEY", "phc_test");