95 lines
4.0 KiB
TypeScript
95 lines
4.0 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
import { initMobileSentry, __resetMobileSentryForTests } from "./sentry.js";
|
|
|
|
beforeEach(() => {
|
|
__resetMobileSentryForTests();
|
|
});
|
|
|
|
describe("initMobileSentry", () => {
|
|
it("returns false when no DSN is provided", async () => {
|
|
const result = await initMobileSentry({ dsn: undefined, release: undefined, environment: undefined });
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("returns false on the web platform even with a DSN", async () => {
|
|
const load = vi.fn();
|
|
const result = await initMobileSentry(
|
|
{ dsn: "https://abc@sentry.example/1", release: "v1", environment: "prod" },
|
|
{ load, getPlatform: async () => "web" }
|
|
);
|
|
expect(result).toBe(false);
|
|
expect(load).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns false when the SDK fails to load", async () => {
|
|
const result = await initMobileSentry(
|
|
{ dsn: "https://abc@sentry.example/1", release: "v1", environment: "prod" },
|
|
{ load: async () => null, getPlatform: async () => "ios" }
|
|
);
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("initializes the SDK with the documented config on native platforms", async () => {
|
|
const init = vi.fn();
|
|
const setTag = vi.fn();
|
|
const result = await initMobileSentry(
|
|
{ dsn: "https://abc@sentry.example/1", release: "1.0.0", environment: "staging", tracesSampleRate: 0.25 },
|
|
{ load: async () => ({ init, setTag }), getPlatform: async () => "android" }
|
|
);
|
|
expect(result).toBe(true);
|
|
expect(init).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
dsn: "https://abc@sentry.example/1",
|
|
release: "1.0.0",
|
|
environment: "staging",
|
|
sendDefaultPii: false,
|
|
tracesSampleRate: 0.25,
|
|
})
|
|
);
|
|
expect(setTag).toHaveBeenCalledWith("platform", "android");
|
|
});
|
|
|
|
it("falls back to the default 0.1 sample rate when none is provided", async () => {
|
|
const init = vi.fn();
|
|
await initMobileSentry(
|
|
{ dsn: "https://abc@sentry.example/1", release: undefined, environment: undefined },
|
|
{ load: async () => ({ init, setTag: vi.fn() }), getPlatform: async () => "ios" }
|
|
);
|
|
expect(init).toHaveBeenCalledWith(expect.objectContaining({ tracesSampleRate: 0.1 }));
|
|
});
|
|
|
|
it("is idempotent — a second call short-circuits", async () => {
|
|
const init = vi.fn();
|
|
const deps = { load: async () => ({ init, setTag: vi.fn() }), getPlatform: async () => "ios" as const };
|
|
await initMobileSentry({ dsn: "https://abc@sentry.example/1", release: undefined, environment: undefined }, deps);
|
|
await initMobileSentry({ dsn: "https://abc@sentry.example/1", release: undefined, environment: undefined }, deps);
|
|
expect(init).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("clamps a sample rate above 1.0 down to 1.0", async () => {
|
|
const init = vi.fn();
|
|
await initMobileSentry(
|
|
{ dsn: "https://abc@sentry.example/1", release: undefined, environment: undefined, tracesSampleRate: 5 },
|
|
{ load: async () => ({ init, setTag: vi.fn() }), getPlatform: async () => "ios" }
|
|
);
|
|
expect(init).toHaveBeenCalledWith(expect.objectContaining({ tracesSampleRate: 1 }));
|
|
});
|
|
|
|
it("clamps negative and non-finite sample rates to safe values", async () => {
|
|
const negativeInit = vi.fn();
|
|
await initMobileSentry(
|
|
{ dsn: "https://abc@sentry.example/1", release: undefined, environment: undefined, tracesSampleRate: -1 },
|
|
{ load: async () => ({ init: negativeInit, setTag: vi.fn() }), getPlatform: async () => "ios" }
|
|
);
|
|
expect(negativeInit).toHaveBeenCalledWith(expect.objectContaining({ tracesSampleRate: 0 }));
|
|
|
|
__resetMobileSentryForTests();
|
|
const nanInit = vi.fn();
|
|
await initMobileSentry(
|
|
{ dsn: "https://abc@sentry.example/1", release: undefined, environment: undefined, tracesSampleRate: Number.NaN },
|
|
{ load: async () => ({ init: nanInit, setTag: vi.fn() }), getPlatform: async () => "ios" }
|
|
);
|
|
expect(nanInit).toHaveBeenCalledWith(expect.objectContaining({ tracesSampleRate: 0.1 }));
|
|
});
|
|
});
|