Skip to content

Commit 8d62052

Browse files
committed
making progress
1 parent 2dbc5d6 commit 8d62052

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

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

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ vi.mock('@sentry/node', async () => {
2020
};
2121
});
2222

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

2538
vi.mock('@sentry/utils', async () => {
@@ -34,27 +47,79 @@ function getById(_id?: string) {
3447
throw new Error('error');
3548
}
3649

50+
const MOCK_LOAD_ARGS: any = {
51+
params: { id: '1' },
52+
route: {
53+
id: '/users/[id]',
54+
},
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+
3776
describe('wrapLoadWithSentry', () => {
3877
beforeEach(() => {
3978
mockCaptureException.mockClear();
4079
mockAddExceptionMechanism.mockClear();
80+
mockTrace.mockClear();
4181
mockScope = new Scope();
4282
});
4383

44-
it('calls captureException', async () => {
84+
it.only('calls captureException', async () => {
4585
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
4686
return {
4787
post: getById(params.id),
4888
};
4989
}
5090

5191
const wrappedLoad = wrapLoadWithSentry(load);
52-
const res = wrappedLoad({ params: { id: '1' } } as any);
92+
const res = wrappedLoad(MOCK_LOAD_ARGS);
5393
await expect(res).rejects.toThrow();
5494

95+
// create promise that waits for timeout
96+
await new Promise(resolve => setTimeout(resolve, 1000, 'timeout'));
97+
5598
expect(mockCaptureException).toHaveBeenCalledTimes(1);
5699
});
57100

101+
it('calls trace function', async () => {
102+
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
103+
return {
104+
post: params.id,
105+
};
106+
}
107+
108+
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);
116+
117+
expect(mockTrace).toHaveBeenCalledTimes(1);
118+
expect(mockTrace).toHaveBeenCalledWith({
119+
op: 'function.sveltekit.load',
120+
});
121+
});
122+
58123
describe('with error() helper', () => {
59124
it.each([
60125
// [statusCode, timesCalled]

0 commit comments

Comments
 (0)