-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathload.test.ts
175 lines (142 loc) · 5.22 KB
/
load.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { beforeEach, describe, expect, it, vi } from 'vitest';
import * as SentrySvelte from '@sentry/svelte';
import type { Load } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import { wrapLoadWithSentry } from '../../src/client/load';
const mockCaptureException = vi.spyOn(SentrySvelte, 'captureException').mockImplementation(() => 'xx');
const mockStartSpan = vi.fn();
vi.mock('@sentry/core', async () => {
const original = (await vi.importActual('@sentry/core')) as any;
return {
...original,
startSpan: (...args: unknown[]) => {
mockStartSpan(...args);
return original.startSpan(...args);
},
};
});
function getById(_id?: string) {
throw new Error('error');
}
const MOCK_LOAD_ARGS: any = {
params: { id: '123' },
route: {
id: '/users/[id]',
},
url: new URL('http://localhost:3000/users/123'),
};
describe('wrapLoadWithSentry', () => {
beforeEach(() => {
mockCaptureException.mockClear();
mockStartSpan.mockClear();
});
it('calls captureException', async () => {
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
post: getById(params.id),
};
}
const wrappedLoad = wrapLoadWithSentry(load);
const res = wrappedLoad(MOCK_LOAD_ARGS);
await expect(res).rejects.toThrow();
expect(mockCaptureException).toHaveBeenCalledTimes(1);
});
it("doesn't call captureException for thrown `Redirect`s", async () => {
async function load(_: Parameters<Load>[0]): Promise<ReturnType<Load>> {
throw redirect(300, 'other/route');
}
const wrappedLoad = wrapLoadWithSentry(load);
const res = wrappedLoad(MOCK_LOAD_ARGS);
await expect(res).rejects.toThrow();
expect(mockCaptureException).not.toHaveBeenCalled();
});
it.each([400, 404, 499])("doesn't call captureException for thrown `HttpError`s with status %s", async status => {
async function load(_: Parameters<Load>[0]): Promise<ReturnType<Load>> {
throw { status, body: 'error' };
}
const wrappedLoad = wrapLoadWithSentry(load);
const res = wrappedLoad(MOCK_LOAD_ARGS);
await expect(res).rejects.toThrow();
expect(mockCaptureException).not.toHaveBeenCalled();
});
it.each([500, 501, 599])('calls captureException for thrown `HttpError`s with status %s', async status => {
async function load(_: Parameters<Load>[0]): Promise<ReturnType<Load>> {
throw { status, body: 'error' };
}
const wrappedLoad = wrapLoadWithSentry(load);
const res = wrappedLoad(MOCK_LOAD_ARGS);
await expect(res).rejects.toThrow();
expect(mockCaptureException).toHaveBeenCalledTimes(1);
});
describe('calls trace function', async () => {
it('creates a load span', async () => {
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
post: params.id,
};
}
const wrappedLoad = wrapLoadWithSentry(load);
await wrappedLoad(MOCK_LOAD_ARGS);
expect(mockStartSpan).toHaveBeenCalledTimes(1);
expect(mockStartSpan).toHaveBeenCalledWith(
{
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
},
op: 'function.sveltekit.load',
name: '/users/[id]',
},
expect.any(Function),
);
});
it("falls back to the raw URL if `even.route.id` isn't available", async () => {
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
post: params.id,
};
}
const wrappedLoad = wrapLoadWithSentry(load);
const event = { ...MOCK_LOAD_ARGS };
delete event.route.id;
await wrappedLoad(MOCK_LOAD_ARGS);
expect(mockStartSpan).toHaveBeenCalledTimes(1);
expect(mockStartSpan).toHaveBeenCalledWith(
{
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
},
op: 'function.sveltekit.load',
name: '/users/123',
},
expect.any(Function),
);
});
});
it('adds an exception mechanism', async () => {
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
post: getById(params.id),
};
}
const wrappedLoad = wrapLoadWithSentry(load);
const res = wrappedLoad(MOCK_LOAD_ARGS);
await expect(res).rejects.toThrow();
expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenCalledWith(expect.any(Error), {
mechanism: { handled: false, type: 'sveltekit', data: { function: 'load' } },
});
});
it("doesn't wrap load more than once if the wrapper was applied multiple times", async () => {
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
post: params.id,
};
}
const wrappedLoad = wrapLoadWithSentry(wrapLoadWithSentry(load));
await wrappedLoad(MOCK_LOAD_ARGS);
expect(mockStartSpan).toHaveBeenCalledTimes(1);
});
});