-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
test.ts
291 lines (234 loc) · 11 KB
/
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
import { expect } from '@playwright/test';
import type { Event } from '@sentry/types';
import { sentryTest } from '../../../../utils/fixtures';
import {
getFirstSentryEnvelopeRequest,
getMultipleSentryEnvelopeRequests,
shouldSkipTracingTest,
} from '../../../../utils/helpers';
sentryTest('creates a new trace on each navigation', async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestPath({ testDir: __dirname });
await getFirstSentryEnvelopeRequest<Event>(page, url);
const navigationEvent1 = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`);
const navigationEvent2 = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#bar`);
const navigation1TraceContext = navigationEvent1.contexts?.trace;
const navigation2TraceContext = navigationEvent2.contexts?.trace;
expect(navigation1TraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigation1TraceContext).not.toHaveProperty('parent_span_id');
expect(navigation2TraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigation2TraceContext).not.toHaveProperty('parent_span_id');
expect(navigation1TraceContext?.trace_id).not.toEqual(navigation2TraceContext?.trace_id);
});
sentryTest('error after navigation has navigation traceId', async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestPath({ testDir: __dirname });
// ensure pageload transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);
const navigationEvent = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`);
const navigationTraceContext = navigationEvent.contexts?.trace;
expect(navigationTraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigationTraceContext).not.toHaveProperty('parent_span_id');
const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
await page.locator('#errorBtn').click();
const errorEvent = await errorEventPromise;
const errorTraceContext = errorEvent.contexts?.trace;
expect(errorTraceContext).toEqual({
trace_id: navigationTraceContext?.trace_id,
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
});
sentryTest('error during navigation has new navigation traceId', async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestPath({ testDir: __dirname });
// ensure pageload transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);
const envelopeRequestsPromise = getMultipleSentryEnvelopeRequests<Event>(page, 2);
await page.goto(`${url}#foo`);
await page.locator('#errorBtn').click();
const events = await envelopeRequestsPromise;
const navigationEvent = events.find(event => event.type === 'transaction');
const errorEvent = events.find(event => !event.type);
const navigationTraceContext = navigationEvent?.contexts?.trace;
expect(navigationTraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigationTraceContext).not.toHaveProperty('parent_span_id');
const errorTraceContext = errorEvent?.contexts?.trace;
expect(errorTraceContext).toMatchObject({
op: 'navigation',
trace_id: errorTraceContext?.trace_id,
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
});
sentryTest(
'outgoing fetch request after navigation has navigation traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestPath({ testDir: __dirname });
// ensure pageload transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);
const navigationEvent = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`);
const navigationTraceContext = navigationEvent.contexts?.trace;
expect(navigationTraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigationTraceContext).not.toHaveProperty('parent_span_id');
const requestPromise = page.waitForRequest('http://example.com/*');
await page.locator('#fetchBtn').click();
const request = await requestPromise;
const headers = request.headers();
// sampling decision is deferred b/c of no active span at the time of request
const navigationTraceId = navigationTraceContext?.trace_id;
expect(headers['sentry-trace']).toMatch(new RegExp(`^${navigationTraceId}-[0-9a-f]{16}$`));
expect(headers['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=public,sentry-trace_id=${navigationTraceId}`,
);
},
);
sentryTest(
'outgoing fetch request during navigation has navigation traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestPath({ testDir: __dirname });
// ensure pageload transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);
const navigationEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
const requestPromise = page.waitForRequest('http://example.com/*');
await page.goto(`${url}#foo`);
await page.locator('#fetchBtn').click();
const [navigationEvent, request] = await Promise.all([navigationEventPromise, requestPromise]);
const navigationTraceContext = navigationEvent.contexts?.trace;
expect(navigationTraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigationTraceContext).not.toHaveProperty('parent_span_id');
const headers = request.headers();
// sampling decision is propagated from active span sampling decision
const navigationTraceId = navigationTraceContext?.trace_id;
expect(headers['sentry-trace']).toMatch(new RegExp(`^${navigationTraceId}-[0-9a-f]{16}-1$`));
expect(headers['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=public,sentry-trace_id=${navigationTraceId},sentry-sample_rate=1,sentry-sampled=true`,
);
},
);
sentryTest(
'outgoing XHR request after navigation has navigation traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestPath({ testDir: __dirname });
// ensure pageload transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);
const navigationEvent = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`);
const navigationTraceContext = navigationEvent.contexts?.trace;
expect(navigationTraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigationTraceContext).not.toHaveProperty('parent_span_id');
const xhrPromise = page.waitForRequest('http://example.com/*');
await page.locator('#xhrBtn').click();
const request = await xhrPromise;
const headers = request.headers();
// sampling decision is deferred b/c of no active span at the time of request
const navigationTraceId = navigationTraceContext?.trace_id;
expect(headers['sentry-trace']).toMatch(new RegExp(`^${navigationTraceId}-[0-9a-f]{16}$`));
expect(headers['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=public,sentry-trace_id=${navigationTraceId}`,
);
},
);
sentryTest(
'outgoing XHR request during navigation has navigation traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestPath({ testDir: __dirname });
// ensure pageload transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);
const navigationEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
const requestPromise = page.waitForRequest('http://example.com/*');
await page.goto(`${url}#foo`);
await page.locator('#xhrBtn').click();
const [navigationEvent, request] = await Promise.all([navigationEventPromise, requestPromise]);
const navigationTraceContext = navigationEvent.contexts?.trace;
expect(navigationTraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigationTraceContext).not.toHaveProperty('parent_span_id');
const headers = request.headers();
// sampling decision is propagated from active span sampling decision
const navigationTraceId = navigationTraceContext?.trace_id;
expect(headers['sentry-trace']).toMatch(new RegExp(`^${navigationTraceId}-[0-9a-f]{16}-1$`));
expect(headers['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=public,sentry-trace_id=${navigationTraceId},sentry-sample_rate=1,sentry-sampled=true`,
);
},
);
sentryTest(
'user feedback event after navigation has navigation traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestPath({ testDir: __dirname });
// ensure pageload transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);
const navigationEvent = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`);
const navigationTraceContext = navigationEvent.contexts?.trace;
expect(navigationTraceContext).toMatchObject({
op: 'navigation',
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
expect(navigationTraceContext).not.toHaveProperty('parent_span_id');
const feedbackEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
await page.getByText('Report a Bug').click();
expect(await page.locator(':visible:text-is("Report a Bug")').count()).toEqual(1);
await page.locator('[name="name"]').fill('Jane Doe');
await page.locator('[name="email"]').fill('janedoe@example.org');
await page.locator('[name="message"]').fill('my example feedback');
await page.locator('[data-sentry-feedback] .btn--primary').click();
const feedbackEvent = await feedbackEventPromise;
const feedbackTraceContext = feedbackEvent.contexts?.trace;
expect(feedbackTraceContext).toMatchObject({
op: 'navigation',
trace_id: navigationTraceContext?.trace_id,
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
});
},
);