Skip to content

Commit 6e30ae3

Browse files
committed
rewrite using trace
1 parent 41c68b9 commit 6e30ae3

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

packages/sveltekit/src/client/load.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { trace } from '@sentry/core';
2-
import { captureException, getCurrentHub } from '@sentry/svelte';
3-
import { addExceptionMechanism, isThenable, objectify } from '@sentry/utils';
2+
import { captureException } from '@sentry/svelte';
3+
import { addExceptionMechanism, objectify } from '@sentry/utils';
44
import type { Load } from '@sveltejs/kit';
55

66
function sendErrorToSentry(e: unknown): unknown {

packages/sveltekit/test/client/load.test.ts

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Scope } from '@sentry/svelte';
2-
import type { ServerLoad } from '@sveltejs/kit';
1+
import { addTracingExtensions, Scope } from '@sentry/svelte';
2+
import type { Load } from '@sveltejs/kit';
33
import { vi } from 'vitest';
44

55
import { wrapLoadWithSentry } from '../../src/client/load';
@@ -19,6 +19,19 @@ vi.mock('@sentry/svelte', async () => {
1919
};
2020
});
2121

22+
const mockTrace = vi.fn();
23+
24+
vi.mock('@sentry/core', async () => {
25+
const original = (await vi.importActual('@sentry/core')) as any;
26+
return {
27+
...original,
28+
trace: (...args: unknown[]) => {
29+
mockTrace(...args);
30+
return original.trace(...args);
31+
},
32+
};
33+
});
34+
2235
const mockAddExceptionMechanism = vi.fn();
2336

2437
vi.mock('@sentry/utils', async () => {
@@ -33,22 +46,54 @@ function getById(_id?: string) {
3346
throw new Error('error');
3447
}
3548

49+
const MOCK_LOAD_ARGS: any = {
50+
params: { id: '123' },
51+
route: {
52+
id: '/users/[id]',
53+
},
54+
url: new URL('http://localhost:3000/users/123'),
55+
request: {
56+
headers: {
57+
get: (key: string) => {
58+
if (key === 'sentry-trace') {
59+
return '1234567890abcdef1234567890abcdef-1234567890abcdef-1';
60+
}
61+
62+
if (key === 'baggage') {
63+
return (
64+
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' +
65+
'sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' +
66+
'sentry-trace_id=1234567890abcdef1234567890abcdef,sentry-sample_rate=1'
67+
);
68+
}
69+
70+
return null;
71+
},
72+
},
73+
},
74+
};
75+
76+
beforeAll(() => {
77+
addTracingExtensions();
78+
});
79+
3680
describe('wrapLoadWithSentry', () => {
3781
beforeEach(() => {
3882
mockCaptureException.mockClear();
3983
mockAddExceptionMechanism.mockClear();
84+
mockTrace.mockClear();
4085
mockScope = new Scope();
4186
});
4287

4388
it('calls captureException', async () => {
44-
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
89+
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
4590
return {
4691
post: getById(params.id),
4792
};
4893
}
4994

5095
const wrappedLoad = wrapLoadWithSentry(load);
51-
const res = wrappedLoad({ params: { id: '1' } } as any);
96+
const res = wrappedLoad(MOCK_LOAD_ARGS);
5297
await expect(res).rejects.toThrow();
5398

5499
expect(mockCaptureException).toHaveBeenCalledTimes(1);
@@ -60,14 +105,14 @@ describe('wrapLoadWithSentry', () => {
60105
return mockScope;
61106
});
62107

63-
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
108+
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
64109
return {
65110
post: getById(params.id),
66111
};
67112
}
68113

69114
const wrappedLoad = wrapLoadWithSentry(load);
70-
const res = wrappedLoad({ params: { id: '1' } } as any);
115+
const res = wrappedLoad(MOCK_LOAD_ARGS);
71116
await expect(res).rejects.toThrow();
72117

73118
expect(addEventProcessorSpy).toBeCalledTimes(1);

0 commit comments

Comments
 (0)