Skip to content

Commit 4aab2aa

Browse files
committed
test(tracing): Add test to validate that spans only added when active span exists
1 parent aae6a14 commit 4aab2aa

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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: [Sentry.BrowserTracing({ tracingOrigins: ['http://example.com'], startTransactionOnPageLoad: false })],
9+
tracesSampleRate: 1,
10+
});
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { envelopeUrlRegex, shouldSkipTracingTest } from '../../../../utils/helpers';
5+
6+
sentryTest(
7+
'there should be no span created for fetch requests with no active span',
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+
page.on('request', request => {
17+
expect(envelopeUrlRegex.test(request.url())).toBe(false);
18+
requestCount++;
19+
});
20+
21+
await page.goto(url);
22+
23+
// Here are the requests that should exist:
24+
// 1. HTML page
25+
// 2. Init JS bundle
26+
// 3. Subject JS bundle
27+
// 4 [OPTIONAl] CDN JS bundle
28+
// and then 3 fetch requests
29+
if (process.env.PW_BUNDLE && process.env.PW_BUNDLE.startsWith('bundle_')) {
30+
expect(requestCount).toBe(7);
31+
} else {
32+
expect(requestCount).toBe(6);
33+
}
34+
},
35+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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: [Sentry.BrowserTracing({ tracingOrigins: ['http://example.com'], startTransactionOnPageLoad: false })],
9+
tracesSampleRate: 1,
10+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const xhr_1 = new XMLHttpRequest();
2+
xhr_1.open('GET', 'http://example.com/0');
3+
xhr_1.send();
4+
5+
const xhr_2 = new XMLHttpRequest();
6+
xhr_2.open('GET', 'http://example.com/1');
7+
xhr_2.send();
8+
9+
const xhr_3 = new XMLHttpRequest();
10+
xhr_3.open('GET', 'http://example.com/2');
11+
xhr_3.send();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { shouldSkipTracingTest } from '../../../../utils/helpers';
5+
6+
sentryTest(
7+
'there should be no span created for xhr requests with no active span',
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+
page.on('request', request => {
17+
expect(request.url()).not.toContain(url);
18+
requestCount++;
19+
});
20+
21+
await page.goto(url);
22+
23+
// Here are the requests that should exist:
24+
// 1. HTML page
25+
// 2. Init JS bundle
26+
// 3. Subject JS bundle
27+
// 4 [OPTIONAl] CDN JS bundle
28+
// and then 3 fetch requests
29+
if (process.env.PW_BUNDLE && process.env.PW_BUNDLE.startsWith('bundle_')) {
30+
expect(requestCount).toBe(7);
31+
} else {
32+
expect(requestCount).toBe(6);
33+
}
34+
},
35+
);

dev-packages/browser-integration-tests/utils/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Page, Request } from '@playwright/test';
22
import type { EnvelopeItemType, Event, EventEnvelopeHeaders } from '@sentry/types';
33

4-
const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\//;
4+
export const envelopeUrlRegex = /\.sentry\.io\/api\/\d+\/envelope\//;
55

66
export const envelopeParser = (request: Request | null): unknown[] => {
77
// https://develop.sentry.dev/sdk/envelopes/

0 commit comments

Comments
 (0)