|
| 1 | +import { buildBeforeSend, getDatadogBuildConfig } from "./config"; |
| 2 | + |
| 3 | +jest.mock("~/sentry/anonymizer", () => ({ |
| 4 | + __esModule: true, |
| 5 | + default: { |
| 6 | + filepathRecursiveReplacer: jest.fn((obj: Record<string, unknown>) => { |
| 7 | + obj._anonymized = true; |
| 8 | + }), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock("./ignoreErrors", () => ({ |
| 13 | + shouldIgnoreErrorMessage: jest.fn((msg: string) => msg.includes("IGNORE_ME")), |
| 14 | +})); |
| 15 | + |
| 16 | +describe("datadog config", () => { |
| 17 | + beforeEach(() => { |
| 18 | + jest.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe("getDatadogBuildConfig", () => { |
| 22 | + it("should return config from build globals (null in Jest)", () => { |
| 23 | + const result = getDatadogBuildConfig(); |
| 24 | + expect(result).toEqual({ |
| 25 | + applicationId: null, |
| 26 | + clientToken: null, |
| 27 | + site: null, |
| 28 | + env: null, |
| 29 | + }); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe("buildBeforeSend", () => { |
| 34 | + it("should return false when shouldSend returns false", () => { |
| 35 | + const shouldSend = jest.fn().mockReturnValue(false); |
| 36 | + const beforeSend = buildBeforeSend(shouldSend); |
| 37 | + expect(beforeSend({}, undefined)).toBe(false); |
| 38 | + expect(shouldSend).toHaveBeenCalledTimes(1); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should return true for non-object event", () => { |
| 42 | + const shouldSend = jest.fn().mockReturnValue(true); |
| 43 | + const beforeSend = buildBeforeSend(shouldSend); |
| 44 | + expect(beforeSend(null, undefined)).toBe(true); |
| 45 | + expect(beforeSend("string", undefined)).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should return false when error message matches ignore list", () => { |
| 49 | + const shouldSend = jest.fn().mockReturnValue(true); |
| 50 | + const beforeSend = buildBeforeSend(shouldSend); |
| 51 | + const event = { error: { message: "IGNORE_ME please" } }; |
| 52 | + expect(beforeSend(event, undefined)).toBe(false); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should return false when event.message matches ignore list", () => { |
| 56 | + const shouldSend = jest.fn().mockReturnValue(true); |
| 57 | + const beforeSend = buildBeforeSend(shouldSend); |
| 58 | + expect(beforeSend({ message: "IGNORE_ME" }, undefined)).toBe(false); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should remove server_name from event", () => { |
| 62 | + const shouldSend = jest.fn().mockReturnValue(true); |
| 63 | + const beforeSend = buildBeforeSend(shouldSend); |
| 64 | + const event = { server_name: "my-machine", message: "ok" }; |
| 65 | + expect(beforeSend(event, undefined)).toBe(true); |
| 66 | + expect(event).not.toHaveProperty("server_name"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should call anonymizer and return true for sendable event", () => { |
| 70 | + const anonymizer = jest.requireMock("~/sentry/anonymizer").default; |
| 71 | + const shouldSend = jest.fn().mockReturnValue(true); |
| 72 | + const beforeSend = buildBeforeSend(shouldSend); |
| 73 | + const event: Record<string, unknown> = { message: "Some error" }; |
| 74 | + expect(beforeSend(event, undefined)).toBe(true); |
| 75 | + expect(anonymizer.filepathRecursiveReplacer).toHaveBeenCalledWith(event); |
| 76 | + expect(event._anonymized).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should return true when anonymizer throws (logs and continues)", () => { |
| 80 | + const anonymizer = jest.requireMock("~/sentry/anonymizer").default; |
| 81 | + jest.mocked(anonymizer.filepathRecursiveReplacer).mockImplementationOnce(() => { |
| 82 | + throw new Error("anonymizer failed"); |
| 83 | + }); |
| 84 | + const consoleSpy = jest.spyOn(console, "error").mockImplementation(); |
| 85 | + const shouldSend = jest.fn().mockReturnValue(true); |
| 86 | + const beforeSend = buildBeforeSend(shouldSend); |
| 87 | + const event = { message: "ok" }; |
| 88 | + expect(beforeSend(event, undefined)).toBe(true); |
| 89 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 90 | + "Datadog beforeSend: anonymization failed", |
| 91 | + expect.any(Error), |
| 92 | + ); |
| 93 | + consoleSpy.mockRestore(); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments