Skip to content

Commit e5ede4c

Browse files
authored
test(tracing): Add integration tests for client requests. (#4374)
1 parent cf54c6a commit e5ede4c

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed
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')));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect, Request } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getSentryTransactionRequest } from '../../../../utils/helpers';
5+
6+
sentryTest('should create spans for multiple fetch requests', async ({ getLocalTestPath, page }) => {
7+
const url = await getLocalTestPath({ testDir: __dirname });
8+
9+
const eventData = await getSentryTransactionRequest(page, url);
10+
const requestSpans = eventData.spans?.filter(({ op }) => op === 'http.client');
11+
12+
expect(requestSpans).toHaveLength(3);
13+
14+
requestSpans?.forEach((span, index) =>
15+
expect(span).toMatchObject({
16+
description: `GET http://example.com/${index}`,
17+
parent_span_id: eventData.contexts?.trace.span_id,
18+
span_id: expect.any(String),
19+
start_timestamp: expect.any(Number),
20+
timestamp: expect.any(Number),
21+
trace_id: eventData.contexts?.trace.trace_id,
22+
}),
23+
);
24+
});
25+
26+
sentryTest('should attach `sentry-trace` header to multiple fetch requests', async ({ getLocalTestPath, page }) => {
27+
const url = await getLocalTestPath({ testDir: __dirname });
28+
29+
page.goto(url);
30+
const requests = await Promise.all([0, 1, 2].map(idx => page.waitForRequest(`http://example.com/${idx}`)));
31+
32+
expect(requests).toHaveLength(3);
33+
34+
requests?.forEach(async (request: Request) => {
35+
const requestHeaders = await request.allHeaders();
36+
expect(requestHeaders).toMatchObject({
37+
'sentry-trace': expect.any(String),
38+
});
39+
});
40+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as Sentry from '@sentry/browser';
2+
import { Integrations } from '@sentry/tracing';
3+
4+
window.Sentry = Sentry;
5+
6+
Sentry.init({
7+
dsn: 'https://[email protected]/1337',
8+
integrations: [new Integrations.BrowserTracing({ tracingOrigins: ['http://example.com'] })],
9+
tracesSampleRate: 1,
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title></title>
6+
<script src="{{htmlWebpackPlugin.options.initialization}}"></script>
7+
</head>
8+
<body>
9+
<script src="{{htmlWebpackPlugin.options.subject}}"></script>
10+
</body>
11+
</html>
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();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect, Request } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getSentryTransactionRequest } from '../../../../utils/helpers';
5+
6+
sentryTest('should create spans for multiple XHR requests', async ({ getLocalTestPath, page }) => {
7+
const url = await getLocalTestPath({ testDir: __dirname });
8+
9+
const eventData = await getSentryTransactionRequest(page, url);
10+
const requestSpans = eventData.spans?.filter(({ op }) => op === 'http.client');
11+
12+
expect(requestSpans).toHaveLength(3);
13+
14+
requestSpans?.forEach((span, index) =>
15+
expect(span).toMatchObject({
16+
description: `GET http://example.com/${index}`,
17+
parent_span_id: eventData.contexts?.trace.span_id,
18+
span_id: expect.any(String),
19+
start_timestamp: expect.any(Number),
20+
timestamp: expect.any(Number),
21+
trace_id: eventData.contexts?.trace.trace_id,
22+
}),
23+
);
24+
});
25+
26+
sentryTest('should attach `sentry-trace` header to multiple XHR requests', async ({ getLocalTestPath, page }) => {
27+
const url = await getLocalTestPath({ testDir: __dirname });
28+
29+
page.goto(url);
30+
const requests = await Promise.all([0, 1, 2].map(idx => page.waitForRequest(`http://example.com/${idx}`)));
31+
32+
expect(requests).toHaveLength(3);
33+
34+
requests?.forEach(async (request: Request) => {
35+
const requestHeaders = await request.allHeaders();
36+
expect(requestHeaders).toMatchObject({
37+
'sentry-trace': expect.any(String),
38+
});
39+
});
40+
});

0 commit comments

Comments
 (0)