Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/lib/observability/web-vitals.test.ts
T

63 lines
1.9 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { reportWebVitals } from "./web-vitals";
describe("reportWebVitals", () => {
it("registers handlers for CLS/LCP/INP/TTFB/FCP", async () => {
const reporter = vi.fn();
const onCLS = vi.fn();
const onLCP = vi.fn();
const onINP = vi.fn();
const onTTFB = vi.fn();
const onFCP = vi.fn();
await reportWebVitals({
reporter,
loader: async () => ({ onCLS, onLCP, onINP, onTTFB, onFCP }) as unknown as typeof import("web-vitals"),
});
expect(onCLS).toHaveBeenCalledWith(reporter);
expect(onLCP).toHaveBeenCalledWith(reporter);
expect(onINP).toHaveBeenCalledWith(reporter);
expect(onTTFB).toHaveBeenCalledWith(reporter);
expect(onFCP).toHaveBeenCalledWith(reporter);
});
it("silently no-ops when the library throws", async () => {
await expect(
reportWebVitals({
reporter: vi.fn(),
loader: async () => {
throw new Error("nope");
},
})
).resolves.toBeUndefined();
});
it("delivers reported metric values via the reporter callback", async () => {
const reporter = vi.fn();
type CB = (m: { name: string; value: number; rating: string; delta: number; id: string; navigationType: string }) => void;
let captured: CB | null = null;
await reportWebVitals({
reporter,
loader: async () =>
({
onCLS: (cb: CB) => {
captured = cb;
},
onLCP: () => {},
onINP: () => {},
onTTFB: () => {},
}) as unknown as typeof import("web-vitals"),
});
captured?.({
name: "CLS",
value: 0.05,
rating: "good",
delta: 0.05,
id: "v1-cls",
navigationType: "navigate",
});
expect(reporter).toHaveBeenCalledTimes(1);
expect(reporter.mock.calls[0]?.[0]).toMatchObject({ name: "CLS", value: 0.05 });
});
});