diff --git a/apps/web/src/sentry.ts b/apps/web/src/sentry.ts index d705ef7c02..85565673d7 100644 --- a/apps/web/src/sentry.ts +++ b/apps/web/src/sentry.ts @@ -197,6 +197,14 @@ export async function initSentry(sentryConfig: IConfigOptions["sentry"]): Promis Sentry.breadcrumbsIntegration(), Sentry.httpContextIntegration(), Sentry.dedupeIntegration(), + // Element Desktop serves the app from the custom `vector://vector/webapp` origin rather + // than a normal web origin, so its stack frames don't match Element Web's and end up + // ungrouped and unsymbolicated. Normalize both to the same relative form so desktop + // crashes group with, and source-map against, the same release as the web app. + Sentry.rewriteFramesIntegration({ + root: "vector://vector/webapp", + prefix: "app://", + }), ]; Sentry.init({ diff --git a/apps/web/test/unit-tests/sentry-test.ts b/apps/web/test/unit-tests/sentry-test.ts new file mode 100644 index 0000000000..0c81b577ff --- /dev/null +++ b/apps/web/test/unit-tests/sentry-test.ts @@ -0,0 +1,81 @@ +/* +Copyright 2026 PrinceXDev + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import * as Sentry from "@sentry/browser"; + +import { initSentry } from "../../src/sentry"; + +jest.mock("@sentry/browser", () => ({ + init: jest.fn(), + inboundFiltersIntegration: jest.fn().mockReturnValue({ name: "InboundFilters" }), + functionToStringIntegration: jest.fn().mockReturnValue({ name: "FunctionToString" }), + breadcrumbsIntegration: jest.fn().mockReturnValue({ name: "Breadcrumbs" }), + httpContextIntegration: jest.fn().mockReturnValue({ name: "HttpContext" }), + dedupeIntegration: jest.fn().mockReturnValue({ name: "Dedupe" }), + rewriteFramesIntegration: jest.fn().mockReturnValue({ name: "RewriteFrames" }), +})); + +describe("initSentry", () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it("does nothing if no sentry config is given", async () => { + await initSentry(undefined); + expect(Sentry.init).not.toHaveBeenCalled(); + }); + + it("normalizes Element Desktop's vector:// stack frames so they group with Element Web", async () => { + await initSentry({ + dsn: "https://examplePublicKey@o0.ingest.sentry.io/0", + environment: "test", + }); + + expect(Sentry.rewriteFramesIntegration).toHaveBeenCalledWith({ + root: "vector://vector/webapp", + prefix: "app://", + }); + + const { integrations } = jest.mocked(Sentry.init).mock.calls[0][0]!; + expect(integrations).toEqual(expect.arrayContaining([{ name: "RewriteFrames" }])); + }); +}); + +describe("rewriteFramesIntegration output", () => { + // Uses the real Sentry SDK integration (not the mock above) to verify the actual rewritten + // path, since it's easy to get the exact prefix/slash count wrong (e.g. `app:///` here would + // double up with the leading slash already present in the frame, producing `app:////...`). + // Relies on this test file's jsdom environment providing a real `window`, which is what makes + // the SDK's browser root/prefix substitution apply in the first place. + it("rewrites a vector:// frame to a clean app:/// path", async () => { + const RealSentry = jest.requireActual("@sentry/browser"); + + const integration = RealSentry.rewriteFramesIntegration({ + root: "vector://vector/webapp", + prefix: "app://", + }); + + const event: any = { + exception: { + values: [ + { + stacktrace: { + frames: [ + { + filename: "vector://vector/webapp/bundles/abc123/bundle.js", + }, + ], + }, + }, + ], + }, + }; + + const processed = integration.processEvent!(event, {}, {} as any) as any; + expect(processed.exception.values[0].stacktrace.frames[0].filename).toBe("app:///bundles/abc123/bundle.js"); + }); +});