Skip to content

Commit 75ffe17

Browse files
committed
add tests
1 parent b81386a commit 75ffe17

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Integration, EventProcessor, Hub } from '@sentry/types';
2+
3+
export class Undici implements Integration {
4+
/**
5+
* @inheritDoc
6+
*/
7+
public static id: string = 'Undici';
8+
9+
/**
10+
* @inheritDoc
11+
*/
12+
public name: string = Undici.id;
13+
14+
/**
15+
* @inheritDoc
16+
*/
17+
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, _getCurrentHub: () => Hub): void {}
18+
}

packages/sveltekit/src/server/load.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ export function wrapLoadWithSentry(origLoad: ServerLoad): ServerLoad {
6060
const traceparentData = sentryTraceHeader ? extractTraceparentData(sentryTraceHeader) : undefined;
6161
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader);
6262

63+
const routeId = event.route.id;
6364
return trace(
6465
{
6566
op: 'function.sveltekit.load',
66-
name: event.route.id || 'load',
67+
name: routeId ? routeId : event.url.pathname,
6768
status: 'ok',
6869
...traceparentData,
6970
metadata: {
70-
source: 'route',
71+
source: routeId ? 'route' : 'url',
7172
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
7273
},
7374
},

packages/sveltekit/test/server/load.test.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { addTracingExtensions } from '@sentry/core';
12
import { Scope } from '@sentry/node';
23
import type { ServerLoad } from '@sveltejs/kit';
34
import { error } from '@sveltejs/kit';
@@ -26,7 +27,7 @@ vi.mock('@sentry/core', async () => {
2627
const original = (await vi.importActual('@sentry/core')) as any;
2728
return {
2829
...original,
29-
trace: (...args) => {
30+
trace: (...args: unknown[]) => {
3031
mockTrace(...args);
3132
return original.trace(...args);
3233
},
@@ -48,10 +49,11 @@ function getById(_id?: string) {
4849
}
4950

5051
const MOCK_LOAD_ARGS: any = {
51-
params: { id: '1' },
52+
params: { id: '123' },
5253
route: {
5354
id: '/users/[id]',
5455
},
56+
url: new URL('http://localhost:3000/users/123'),
5557
request: {
5658
headers: {
5759
get: (key: string) => {
@@ -73,6 +75,10 @@ const MOCK_LOAD_ARGS: any = {
7375
},
7476
};
7577

78+
beforeAll(() => {
79+
addTracingExtensions();
80+
});
81+
7682
describe('wrapLoadWithSentry', () => {
7783
beforeEach(() => {
7884
mockCaptureException.mockClear();
@@ -81,7 +87,7 @@ describe('wrapLoadWithSentry', () => {
8187
mockScope = new Scope();
8288
});
8389

84-
it.only('calls captureException', async () => {
90+
it('calls captureException', async () => {
8591
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
8692
return {
8793
post: getById(params.id),
@@ -106,18 +112,33 @@ describe('wrapLoadWithSentry', () => {
106112
}
107113

108114
const wrappedLoad = wrapLoadWithSentry(load);
109-
await wrappedLoad({
110-
params: { id: '1' },
111-
route: {
112-
id: '',
113-
},
114-
headers: { 'sentry-trace': '1234567890abcdef1234567890abcdef-1234567890abcdef-1' },
115-
} as any);
115+
await wrappedLoad(MOCK_LOAD_ARGS);
116116

117117
expect(mockTrace).toHaveBeenCalledTimes(1);
118-
expect(mockTrace).toHaveBeenCalledWith({
119-
op: 'function.sveltekit.load',
120-
});
118+
expect(mockTrace).toHaveBeenCalledWith(
119+
{
120+
op: 'function.sveltekit.load',
121+
name: '/users/[id]',
122+
parentSampled: true,
123+
parentSpanId: '1234567890abcdef',
124+
status: 'ok',
125+
traceId: '1234567890abcdef1234567890abcdef',
126+
metadata: {
127+
dynamicSamplingContext: {
128+
environment: 'production',
129+
public_key: 'dogsarebadatkeepingsecrets',
130+
release: '1.0.0',
131+
sample_rate: '1',
132+
trace_id: '1234567890abcdef1234567890abcdef',
133+
transaction: 'dogpark',
134+
user_segment: 'segmentA',
135+
},
136+
source: 'route',
137+
},
138+
},
139+
expect.any(Function),
140+
expect.any(Function),
141+
);
121142
});
122143

123144
describe('with error() helper', () => {
@@ -140,7 +161,7 @@ describe('wrapLoadWithSentry', () => {
140161
}
141162

142163
const wrappedLoad = wrapLoadWithSentry(load);
143-
const res = wrappedLoad({ params: { id: '1' } } as any);
164+
const res = wrappedLoad(MOCK_LOAD_ARGS);
144165
await expect(res).rejects.toThrow();
145166

146167
expect(mockCaptureException).toHaveBeenCalledTimes(times);
@@ -160,7 +181,7 @@ describe('wrapLoadWithSentry', () => {
160181
}
161182

162183
const wrappedLoad = wrapLoadWithSentry(load);
163-
const res = wrappedLoad({ params: { id: '1' } } as any);
184+
const res = wrappedLoad(MOCK_LOAD_ARGS);
164185
await expect(res).rejects.toThrow();
165186

166187
expect(addEventProcessorSpy).toBeCalledTimes(1);

0 commit comments

Comments
 (0)