Skip to content

Commit 8abd97c

Browse files
authored
test(V7/browser-integration-tests): Check for sentry-trace header in TwP (#11555)
1 parent f6b284d commit 8abd97c

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module.exports = [
131131
name: '@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped)',
132132
path: 'packages/browser/build/bundles/bundle.tracing.es5.min.js',
133133
gzip: true,
134-
limit: '40 KB',
134+
limit: '41 KB',
135135
},
136136

137137
// React

dev-packages/browser-integration-tests/suites/tracing/request/fetch-with-no-active-span/init.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ window.Sentry = Sentry;
55
Sentry.init({
66
dsn: 'https://[email protected]/1337',
77
// disable pageload transaction
8-
integrations: [Sentry.BrowserTracing({ tracingOrigins: ['http://example.com'], startTransactionOnPageLoad: false })],
8+
integrations: [
9+
new Sentry.BrowserTracing({ tracingOrigins: ['http://example.com'], startTransactionOnPageLoad: false }),
10+
],
911
tracesSampleRate: 1,
1012
});

dev-packages/browser-integration-tests/suites/tracing/request/fetch-with-no-active-span/test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { sentryTest } from '../../../../utils/fixtures';
44
import { envelopeUrlRegex, shouldSkipTracingTest } from '../../../../utils/helpers';
55

66
sentryTest(
7-
'there should be no span created for fetch requests with no active span',
7+
'should not create span for fetch requests with no active span but should attach sentry-trace header',
88
async ({ getLocalTestPath, page }) => {
99
if (shouldSkipTracingTest()) {
1010
sentryTest.skip();
@@ -13,7 +13,12 @@ sentryTest(
1313
const url = await getLocalTestPath({ testDir: __dirname });
1414

1515
let requestCount = 0;
16+
const sentryTraceHeaders: string[] = [];
1617
page.on('request', request => {
18+
const sentryTraceHeader = request.headers()['sentry-trace'];
19+
if (sentryTraceHeader) {
20+
sentryTraceHeaders.push(sentryTraceHeader);
21+
}
1722
expect(envelopeUrlRegex.test(request.url())).toBe(false);
1823
requestCount++;
1924
});
@@ -31,5 +36,12 @@ sentryTest(
3136
} else {
3237
expect(requestCount).toBe(6);
3338
}
39+
40+
expect(sentryTraceHeaders).toHaveLength(3);
41+
expect(sentryTraceHeaders).toEqual([
42+
expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/),
43+
expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/),
44+
expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})$/),
45+
]);
3446
},
3547
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
// disable pageload transaction
8+
integrations: [new Sentry.BrowserTracing({ tracePropagationTargets: ['http://example.com'] })],
9+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fetch('http://example.com/0').then(fetch('http://example.com/1').then(fetch('http://example.com/2')));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { envelopeUrlRegex, shouldSkipTracingTest } from '../../../../utils/helpers';
5+
6+
sentryTest(
7+
'should not create span for fetch requests with no active span but should attach sentry-trace header if no sample rate is set',
8+
async ({ getLocalTestPath, page }) => {
9+
if (shouldSkipTracingTest()) {
10+
sentryTest.skip();
11+
}
12+
13+
const url = await getLocalTestPath({ testDir: __dirname });
14+
15+
let requestCount = 0;
16+
const sentryTraceHeaders: string[] = [];
17+
page.on('request', request => {
18+
const sentryTraceHeader = request.headers()['sentry-trace'];
19+
if (sentryTraceHeader) {
20+
sentryTraceHeaders.push(sentryTraceHeader);
21+
}
22+
expect(envelopeUrlRegex.test(request.url())).toBe(false);
23+
requestCount++;
24+
});
25+
26+
await page.goto(url);
27+
28+
// Here are the requests that should exist:
29+
// 1. HTML page
30+
// 2. Init JS bundle
31+
// 3. Subject JS bundle
32+
// 4 [OPTIONAl] CDN JS bundle
33+
// and then 3 fetch requests
34+
if (process.env.PW_BUNDLE && process.env.PW_BUNDLE.startsWith('bundle_')) {
35+
expect(requestCount).toBe(7);
36+
} else {
37+
expect(requestCount).toBe(6);
38+
}
39+
40+
// TODO: This is incorrect behavior. Even if `tracesSampleRate` is not set (which in browser
41+
// realistically is the only way to truly get "Tracing without performance"), we should still
42+
// attach the `sentry-trace` header to the fetch requests.
43+
// Right now, we don't do this, as this test demonstrates.
44+
expect(sentryTraceHeaders).toHaveLength(0);
45+
},
46+
);

0 commit comments

Comments
 (0)