Skip to content

Commit a338b1b

Browse files
authored
test(tracing): Add tests for pageload spans. (#4387)
1 parent 3d09348 commit a338b1b

File tree

8 files changed

+92
-0
lines changed

8 files changed

+92
-0
lines changed
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()],
9+
tracesSampleRate: 1,
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getSentryTransactionRequest } from '../../../../utils/helpers';
5+
6+
sentryTest('should add browser-related spans to pageload transaction', async ({ getLocalTestPath, page }) => {
7+
const url = await getLocalTestPath({ testDir: __dirname });
8+
9+
const eventData = await getSentryTransactionRequest(page, url);
10+
const browserSpans = eventData.spans?.filter(({ op }) => op === 'browser');
11+
12+
// Spans `connect`, `cache` and `DNS` are not always inside `pageload` transaction.
13+
expect(browserSpans?.length).toBeGreaterThanOrEqual(4);
14+
15+
['domContentLoadedEvent', 'loadEvent', 'request', 'response'].forEach(eventDesc =>
16+
expect(browserSpans).toContainEqual(
17+
expect.objectContaining({
18+
description: eventDesc,
19+
parent_span_id: eventData.contexts?.trace.span_id,
20+
}),
21+
),
22+
);
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(() => {})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p {
2+
color: red;
3+
text-align: center;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
<img src="https://example.com/path/to/image.svg">
10+
<script src="https://example.com/path/to/script.js"></script>
11+
<link href="https://example.com/path/to/style.css" type="text/css" rel="stylesheet">
12+
<span>Rendered</span>
13+
</body>
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect, Route } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getSentryTransactionRequest } from '../../../../utils/helpers';
5+
6+
sentryTest('should add resource spans to pageload transaction', async ({ getLocalTestPath, page }) => {
7+
// Intercepting asset requests to avoid network-related flakiness and random retries (on Firefox).
8+
await page.route('**/path/to/image.svg', (route: Route) => route.fulfill({ path: `${__dirname}/assets/image.svg` }));
9+
await page.route('**/path/to/script.js', (route: Route) => route.fulfill({ path: `${__dirname}/assets/script.js` }));
10+
await page.route('**/path/to/style.css', (route: Route) => route.fulfill({ path: `${__dirname}/assets/style.css` }));
11+
12+
const url = await getLocalTestPath({ testDir: __dirname });
13+
14+
const eventData = await getSentryTransactionRequest(page, url);
15+
const resourceSpans = eventData.spans?.filter(({ op }) => op?.startsWith('resource'));
16+
17+
expect(resourceSpans?.length).toBe(3);
18+
19+
['resource.img', 'resource.script', 'resource.link'].forEach(op =>
20+
expect(resourceSpans).toContainEqual(
21+
expect.objectContaining({
22+
op: op,
23+
parent_span_id: eventData.contexts?.trace.span_id,
24+
}),
25+
),
26+
);
27+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
<span>Rendered</span>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)