-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbrowserTracingIntegration.test.ts
304 lines (253 loc) · 11.7 KB
/
browserTracingIntegration.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* @vitest-environment jsdom
*/
/* eslint-disable @typescript-eslint/unbound-method */
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Span } from '@sentry/core';
import { writable } from 'svelte/store';
import { navigating, page } from '$app/stores';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
import { browserTracingIntegration } from '../../src/client';
import * as SentrySvelte from '@sentry/svelte';
// we have to overwrite the global mock from `vitest.setup.ts` here to reset the
// `navigating` store for each test.
vi.mock('$app/stores', async () => {
return {
get navigating() {
return navigatingStore;
},
page: writable(),
};
});
let navigatingStore = writable();
describe('browserTracingIntegration', () => {
const svelteBrowserTracingIntegrationSpy = vi.spyOn(SentrySvelte, 'browserTracingIntegration');
let createdRootSpan: Partial<Span> | undefined;
// @ts-expect-error - only returning a partial span here, that's fine
vi.spyOn(SentrySvelte, 'getActiveSpan').mockImplementation(() => {
return createdRootSpan;
});
const startBrowserTracingPageLoadSpanSpy = vi
.spyOn(SentrySvelte, 'startBrowserTracingPageLoadSpan')
.mockImplementation((_client, txnCtx) => {
createdRootSpan = {
...txnCtx,
updateName: vi.fn(),
setAttribute: vi.fn(),
};
return createdRootSpan as Span;
});
const startBrowserTracingNavigationSpanSpy = vi
.spyOn(SentrySvelte, 'startBrowserTracingNavigationSpan')
.mockImplementation((_client, txnCtx) => {
createdRootSpan = {
...txnCtx,
updateName: vi.fn(),
setAttribute: vi.fn(),
};
return createdRootSpan as Span;
});
const fakeClient = { getOptions: () => ({}), on: () => {}, addEventProcessor: () => {} };
const mockedRoutingSpan = {
end: () => {},
};
const routingSpanEndSpy = vi.spyOn(mockedRoutingSpan, 'end');
// @ts-expect-error - mockedRoutingSpan is not a complete Span, that's fine
const startInactiveSpanSpy = vi.spyOn(SentrySvelte, 'startInactiveSpan').mockImplementation(() => mockedRoutingSpan);
beforeEach(() => {
createdRootSpan = undefined;
navigatingStore = writable();
vi.clearAllMocks();
});
it('implements required hooks', () => {
const integration = browserTracingIntegration();
expect(integration.name).toEqual('BrowserTracing');
expect(integration.afterAllSetup).toBeDefined();
});
it('passes on the options to the original integration', () => {
browserTracingIntegration({ enableLongTask: true, idleTimeout: 4242 });
expect(svelteBrowserTracingIntegrationSpy).toHaveBeenCalledTimes(1);
expect(svelteBrowserTracingIntegrationSpy).toHaveBeenCalledWith({
enableLongTask: true,
idleTimeout: 4242,
instrumentNavigation: false,
instrumentPageLoad: false,
});
});
it('always disables `instrumentNavigation` and `instrumentPageLoad` in the original integration', () => {
browserTracingIntegration({ instrumentNavigation: true, instrumentPageLoad: true });
expect(svelteBrowserTracingIntegrationSpy).toHaveBeenCalledTimes(1);
// This is fine and expected because we don't want to start the default instrumentation
// SvelteKit's browserTracingIntegration takes care of instrumenting pageloads and navigations on its own.
expect(svelteBrowserTracingIntegrationSpy).toHaveBeenCalledWith({
instrumentNavigation: false,
instrumentPageLoad: false,
});
});
it("starts a pageload span when it's called with default params", () => {
const integration = browserTracingIntegration();
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
integration.afterAllSetup(fakeClient);
expect(startBrowserTracingPageLoadSpanSpy).toHaveBeenCalledTimes(1);
expect(startBrowserTracingPageLoadSpanSpy).toHaveBeenCalledWith(fakeClient, {
name: '/',
op: 'pageload',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.sveltekit',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
},
});
// We emit an update to the `page` store to simulate the SvelteKit router lifecycle
// @ts-expect-error - page is a writable but the types say it's just readable
page.set({ route: { id: 'testRoute' } });
// This should update the transaction name with the parameterized route:
expect(createdRootSpan?.updateName).toHaveBeenCalledTimes(1);
expect(createdRootSpan?.updateName).toHaveBeenCalledWith('testRoute');
expect(createdRootSpan?.setAttribute).toHaveBeenCalledWith(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
});
it("doesn't start a pageload span if `instrumentPageLoad` is false", () => {
const integration = browserTracingIntegration({
instrumentPageLoad: false,
});
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
integration.afterAllSetup(fakeClient);
expect(startBrowserTracingPageLoadSpanSpy).toHaveBeenCalledTimes(0);
});
it("updates the current scope's transactionName once it's resolved during pageload", () => {
const scopeSetTransactionNameSpy = vi.fn();
// @ts-expect-error - only returning a partial scope here, that's fine
vi.spyOn(SentrySvelte, 'getCurrentScope').mockImplementation(() => {
return {
setTransactionName: scopeSetTransactionNameSpy,
};
});
const integration = browserTracingIntegration();
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
integration.afterAllSetup(fakeClient);
// We emit an update to the `page` store to simulate the SvelteKit router lifecycle
// @ts-expect-error - page is a writable but the types say it's just readable
page.set({ route: { id: 'testRoute/:id' } });
// This should update the transaction name with the parameterized route:
expect(scopeSetTransactionNameSpy).toHaveBeenCalledTimes(3);
expect(scopeSetTransactionNameSpy).toHaveBeenLastCalledWith('testRoute/:id');
});
it("doesn't start a navigation span when `instrumentNavigation` is false", () => {
const integration = browserTracingIntegration({
instrumentNavigation: false,
});
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
integration.afterAllSetup(fakeClient);
// We emit an update to the `navigating` store to simulate the SvelteKit navigation lifecycle
// @ts-expect-error - page is a writable but the types say it's just readable
navigating.set({
from: { route: { id: '/users' }, url: { pathname: '/users' } },
to: { route: { id: '/users/[id]' }, url: { pathname: '/users/7762' } },
});
// This should update the transaction name with the parameterized route:
expect(startBrowserTracingNavigationSpanSpy).toHaveBeenCalledTimes(0);
});
it('starts a navigation span when `startTransactionOnLocationChange` is true', () => {
const integration = browserTracingIntegration({
instrumentPageLoad: false,
});
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
integration.afterAllSetup(fakeClient);
// We emit an update to the `navigating` store to simulate the SvelteKit navigation lifecycle
// @ts-expect-error - page is a writable but the types say it's just readable
navigating.set({
from: { route: { id: '/users' }, url: { pathname: '/users' } },
to: { route: { id: '/users/[id]' }, url: { pathname: '/users/7762' } },
type: 'link',
});
// This should update the transaction name with the parameterized route:
expect(startBrowserTracingNavigationSpanSpy).toHaveBeenCalledTimes(1);
expect(startBrowserTracingNavigationSpanSpy).toHaveBeenCalledWith(fakeClient, {
name: '/users/[id]',
op: 'navigation',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.sveltekit',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
'sentry.sveltekit.navigation.from': '/users',
'sentry.sveltekit.navigation.to': '/users/[id]',
'sentry.sveltekit.navigation.type': 'link',
},
});
expect(startInactiveSpanSpy).toHaveBeenCalledWith({
op: 'ui.sveltekit.routing',
name: 'SvelteKit Route Change',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.sveltekit',
'sentry.sveltekit.navigation.from': '/users',
'sentry.sveltekit.navigation.to': '/users/[id]',
'sentry.sveltekit.navigation.type': 'link',
},
onlyIfParent: true,
});
// We emit `null` here to simulate the end of the navigation lifecycle
// @ts-expect-error - page is a writable but the types say it's just readable
navigating.set(null);
expect(routingSpanEndSpy).toHaveBeenCalledTimes(1);
});
describe('handling same origin and destination navigations', () => {
it("doesn't start a navigation span if the raw navigation origin and destination are equal", () => {
const integration = browserTracingIntegration({
instrumentPageLoad: false,
});
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
integration.afterAllSetup(fakeClient);
// We emit an update to the `navigating` store to simulate the SvelteKit navigation lifecycle
// @ts-expect-error - page is a writable but the types say it's just readable
navigating.set({
from: { route: { id: '/users/[id]' }, url: { pathname: '/users/7762' } },
to: { route: { id: '/users/[id]' }, url: { pathname: '/users/7762' } },
});
expect(startBrowserTracingNavigationSpanSpy).toHaveBeenCalledTimes(0);
});
it('starts a navigation transaction if the raw navigation origin and destination are not equal', () => {
const integration = browserTracingIntegration({
instrumentPageLoad: false,
});
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
integration.afterAllSetup(fakeClient);
// @ts-expect-error - page is a writable but the types say it's just readable
navigating.set({
from: { route: { id: '/users/[id]' }, url: { pathname: '/users/7762' } },
to: { route: { id: '/users/[id]' }, url: { pathname: '/users/223412' } },
});
expect(startBrowserTracingNavigationSpanSpy).toHaveBeenCalledTimes(1);
expect(startBrowserTracingNavigationSpanSpy).toHaveBeenCalledWith(fakeClient, {
name: '/users/[id]',
op: 'navigation',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.sveltekit',
'sentry.sveltekit.navigation.from': '/users/[id]',
'sentry.sveltekit.navigation.to': '/users/[id]',
},
});
expect(startInactiveSpanSpy).toHaveBeenCalledWith({
op: 'ui.sveltekit.routing',
name: 'SvelteKit Route Change',
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.sveltekit',
'sentry.sveltekit.navigation.from': '/users/[id]',
'sentry.sveltekit.navigation.to': '/users/[id]',
},
onlyIfParent: true,
});
});
it('falls back to `window.location.pathname` to determine the raw origin', () => {
const integration = browserTracingIntegration({
instrumentPageLoad: false,
});
// @ts-expect-error - the fakeClient doesn't satisfy Client but that's fine
integration.afterAllSetup(fakeClient);
// window.location.pathname is "/" in tests
// @ts-expect-error - page is a writable but the types say it's just readable
navigating.set({
to: { route: {}, url: { pathname: '/' } },
});
expect(startBrowserTracingNavigationSpanSpy).toHaveBeenCalledTimes(0);
});
});
});