-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathperformance.test.ts
255 lines (198 loc) · 7.52 KB
/
performance.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
/**
* @vitest-environment jsdom
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { act, render } from '@testing-library/svelte';
import { getClient, getCurrentScope, getIsolationScope, init, startSpan } from '../src';
import type { TransactionEvent } from '@sentry/types';
// @ts-expect-error svelte import
import DummyComponent from './components/Dummy.svelte';
const PUBLIC_DSN = 'https://username@domain/123';
describe('Sentry.trackComponent()', () => {
const transactions: TransactionEvent[] = [];
beforeEach(() => {
transactions.splice(0, transactions.length);
vi.resetAllMocks();
getCurrentScope().clear();
getIsolationScope().clear();
const beforeSendTransaction = vi.fn(event => {
transactions.push(event);
return null;
});
init({
dsn: PUBLIC_DSN,
enableTracing: true,
beforeSendTransaction,
});
});
it('creates init and update spans on component initialization', async () => {
startSpan({ name: 'outer' }, span => {
expect(span).toBeDefined();
render(DummyComponent, { props: { options: {} } });
});
await getClient()?.flush();
expect(transactions).toHaveLength(1);
const transaction = transactions[0]!;
expect(transaction.spans).toHaveLength(2);
const rootSpanId = transaction.contexts?.trace?.span_id;
expect(rootSpanId).toBeDefined();
const initSpanId = transaction.spans![0]?.span_id;
expect(transaction.spans![0]).toEqual({
data: {
'sentry.op': 'ui.svelte.init',
'sentry.origin': 'auto.ui.svelte',
},
description: '<Svelte Component>',
op: 'ui.svelte.init',
origin: 'auto.ui.svelte',
parent_span_id: rootSpanId,
span_id: initSpanId,
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: expect.any(String),
});
expect(transaction.spans![1]).toEqual({
data: {
'sentry.op': 'ui.svelte.update',
'sentry.origin': 'auto.ui.svelte',
},
description: '<Svelte Component>',
op: 'ui.svelte.update',
origin: 'auto.ui.svelte',
parent_span_id: rootSpanId,
span_id: expect.any(String),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: expect.any(String),
});
});
it('creates an update span, when the component is updated', async () => {
startSpan({ name: 'outer' }, async span => {
expect(span).toBeDefined();
// first we create the component
const { component } = render(DummyComponent, { props: { options: {} } });
// then trigger an update
// (just changing the trackUpdates prop so that we trigger an update. #
// The value doesn't do anything here)
await act(() => component.$set({ options: { trackUpdates: true } }));
});
await getClient()?.flush();
expect(transactions).toHaveLength(1);
const transaction = transactions[0]!;
expect(transaction.spans).toHaveLength(3);
const rootSpanId = transaction.contexts?.trace?.span_id;
expect(rootSpanId).toBeDefined();
const initSpanId = transaction.spans![0]?.span_id;
expect(transaction.spans![0]).toEqual({
data: {
'sentry.op': 'ui.svelte.init',
'sentry.origin': 'auto.ui.svelte',
},
description: '<Svelte Component>',
op: 'ui.svelte.init',
origin: 'auto.ui.svelte',
parent_span_id: rootSpanId,
span_id: initSpanId,
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: expect.any(String),
});
expect(transaction.spans![1]).toEqual({
data: {
'sentry.op': 'ui.svelte.update',
'sentry.origin': 'auto.ui.svelte',
},
description: '<Svelte Component>',
op: 'ui.svelte.update',
origin: 'auto.ui.svelte',
parent_span_id: rootSpanId,
span_id: expect.any(String),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: expect.any(String),
});
expect(transaction.spans![2]).toEqual({
data: {
'sentry.op': 'ui.svelte.update',
'sentry.origin': 'auto.ui.svelte',
},
description: '<Svelte Component>',
op: 'ui.svelte.update',
origin: 'auto.ui.svelte',
parent_span_id: rootSpanId,
span_id: expect.any(String),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: expect.any(String),
});
});
it('only creates init spans if trackUpdates is deactivated', async () => {
startSpan({ name: 'outer' }, async span => {
expect(span).toBeDefined();
render(DummyComponent, { props: { options: { trackUpdates: false } } });
});
await getClient()?.flush();
expect(transactions).toHaveLength(1);
const transaction = transactions[0]!;
expect(transaction.spans).toHaveLength(1);
expect(transaction.spans![0]?.op).toEqual('ui.svelte.init');
});
it('only creates update spans if trackInit is deactivated', async () => {
startSpan({ name: 'outer' }, span => {
expect(span).toBeDefined();
render(DummyComponent, { props: { options: { trackInit: false } } });
});
await getClient()?.flush();
expect(transactions).toHaveLength(1);
const transaction = transactions[0]!;
expect(transaction.spans).toHaveLength(1);
expect(transaction.spans![0]?.op).toEqual('ui.svelte.update');
});
it('creates no spans if trackInit and trackUpdates are deactivated', async () => {
startSpan({ name: 'outer' }, span => {
expect(span).toBeDefined();
render(DummyComponent, { props: { options: { trackInit: false, trackUpdates: false } } });
});
await getClient()?.flush();
expect(transactions).toHaveLength(1);
const transaction = transactions[0]!;
expect(transaction.spans).toHaveLength(0);
});
it('sets a custom component name as a span name if `componentName` is provided', async () => {
startSpan({ name: 'outer' }, span => {
expect(span).toBeDefined();
render(DummyComponent, {
props: { options: { componentName: 'CustomComponentName' } },
});
});
await getClient()?.flush();
expect(transactions).toHaveLength(1);
const transaction = transactions[0]!;
expect(transaction.spans).toHaveLength(2);
expect(transaction.spans![0]?.description).toEqual('<CustomComponentName>');
expect(transaction.spans![1]?.description).toEqual('<CustomComponentName>');
});
it("doesn't do anything, if there's no ongoing transaction", async () => {
render(DummyComponent, {
props: { options: { componentName: 'CustomComponentName' } },
});
await getClient()?.flush();
expect(transactions).toHaveLength(0);
});
it("doesn't record update spans, if there's no ongoing root span at that time", async () => {
const component = startSpan({ name: 'outer' }, span => {
expect(span).toBeDefined();
const { component } = render(DummyComponent, { props: { options: {} } });
return component;
});
// then trigger an update after the root span ended - should not record update span
await act(() => component.$set({ options: { trackUpdates: true } }));
await getClient()?.flush();
expect(transactions).toHaveLength(1);
const transaction = transactions[0]!;
// One update span is triggered by the initial rendering, but the second one is not captured
expect(transaction.spans).toHaveLength(2);
expect(transaction.spans![0]?.op).toEqual('ui.svelte.init');
expect(transaction.spans![1]?.op).toEqual('ui.svelte.update');
});
});