|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mockCapture = vi.hoisted(() => vi.fn()); |
| 4 | +const mockCaptureException = vi.hoisted(() => vi.fn()); |
| 5 | +const mockIdentify = vi.hoisted(() => vi.fn()); |
| 6 | +const mockShutdown = vi.hoisted(() => vi.fn()); |
| 7 | +const MockPostHog = vi.hoisted(() => vi.fn()); |
| 8 | + |
| 9 | +vi.mock("posthog-node", () => ({ PostHog: MockPostHog })); |
| 10 | + |
| 11 | +import { |
| 12 | + captureException, |
| 13 | + initializePostHog, |
| 14 | + resetUser, |
| 15 | + shutdownPostHog, |
| 16 | + trackAppEvent, |
| 17 | +} from "./posthog-analytics"; |
| 18 | + |
| 19 | +describe("posthog-analytics", () => { |
| 20 | + beforeEach(() => { |
| 21 | + vi.clearAllMocks(); |
| 22 | + MockPostHog.mockImplementation(function (this: Record<string, unknown>) { |
| 23 | + this.capture = mockCapture; |
| 24 | + this.captureException = mockCaptureException; |
| 25 | + this.identify = mockIdentify; |
| 26 | + this.shutdown = mockShutdown; |
| 27 | + }); |
| 28 | + process.env.VITE_POSTHOG_API_KEY = "test-key"; |
| 29 | + resetUser(); |
| 30 | + initializePostHog(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + await shutdownPostHog(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("includes the app version on every tracked event", () => { |
| 38 | + trackAppEvent("app_started"); |
| 39 | + |
| 40 | + expect(mockCapture).toHaveBeenCalledWith( |
| 41 | + expect.objectContaining({ |
| 42 | + event: "app_started", |
| 43 | + properties: expect.objectContaining({ |
| 44 | + team: "posthog-code", |
| 45 | + app_version: "0.0.0-test", |
| 46 | + }), |
| 47 | + }), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it("lets caller-supplied properties coexist with the app version", () => { |
| 52 | + trackAppEvent("app_quit", { reason: "user-initiated" }); |
| 53 | + |
| 54 | + expect(mockCapture).toHaveBeenCalledWith( |
| 55 | + expect.objectContaining({ |
| 56 | + properties: expect.objectContaining({ |
| 57 | + reason: "user-initiated", |
| 58 | + app_version: "0.0.0-test", |
| 59 | + }), |
| 60 | + }), |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it("does not let caller-supplied app_version override the system value", () => { |
| 65 | + trackAppEvent("app_quit", { app_version: "spoofed" }); |
| 66 | + |
| 67 | + expect(mockCapture).toHaveBeenCalledWith( |
| 68 | + expect.objectContaining({ |
| 69 | + properties: expect.objectContaining({ |
| 70 | + app_version: "0.0.0-test", |
| 71 | + }), |
| 72 | + }), |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("includes the app version on captured exceptions", () => { |
| 77 | + captureException(new Error("boom")); |
| 78 | + |
| 79 | + expect(mockCaptureException).toHaveBeenCalledWith( |
| 80 | + expect.any(Error), |
| 81 | + expect.any(String), |
| 82 | + expect.objectContaining({ |
| 83 | + team: "posthog-code", |
| 84 | + app_version: "0.0.0-test", |
| 85 | + }), |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it("does not let additionalProperties override app_version on exceptions", () => { |
| 90 | + captureException(new Error("boom"), { app_version: "spoofed" }); |
| 91 | + |
| 92 | + expect(mockCaptureException).toHaveBeenCalledWith( |
| 93 | + expect.any(Error), |
| 94 | + expect.any(String), |
| 95 | + expect.objectContaining({ |
| 96 | + app_version: "0.0.0-test", |
| 97 | + }), |
| 98 | + ); |
| 99 | + }); |
| 100 | +}); |
0 commit comments