Skip to content

Commit b49aeb3

Browse files
committed
Add integration tests for startTransaction
1 parent 1c3f85a commit b49aeb3

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const transaction = Sentry.startTransaction({ name: 'test_transaction_1' });
2+
const span_1 = transaction.startChild({
3+
op: 'span_1',
4+
data: {
5+
foo: 'bar',
6+
baz: [1, 2, 3],
7+
},
8+
});
9+
for (let i = 0; i < 2000; i++);
10+
11+
// span_1 finishes
12+
span_1.finish();
13+
14+
// span_2 doesn't finish
15+
const span_2 = transaction.startChild({ op: 'span_2' });
16+
for (let i = 0; i < 4000; i++);
17+
18+
const span_3 = transaction.startChild({ op: 'span_3' });
19+
for (let i = 0; i < 4000; i++);
20+
21+
// span_4 is the child of span_3 but doesn't finish.
22+
const span_4 = span_3.startChild({ op: 'span_4', data: { qux: 'quux' } });
23+
24+
// span_5 is another child of span_3 but finishes.
25+
const span_5 = span_3.startChild({ op: 'span_5' }).finish();
26+
27+
// span_3 also finishes
28+
span_3.finish();
29+
30+
transaction.finish();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect } from '@playwright/test';
2+
import { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getSentryTransactionRequest } from '../../../../utils/helpers';
6+
7+
sentryTest('should report a transaction in an envelope', async ({ getLocalTestPath, page }) => {
8+
const url = await getLocalTestPath({ testDir: __dirname });
9+
const eventData = await getSentryTransactionRequest(page, url);
10+
const transaction = eventData[2] as Event;
11+
12+
expect(transaction.transaction).toBe('test_transaction_1');
13+
expect(transaction.spans).toBeDefined();
14+
});
15+
16+
sentryTest('should report finished spans as children of the root transaction', async ({ getLocalTestPath, page }) => {
17+
const url = await getLocalTestPath({ testDir: __dirname });
18+
const eventData = await getSentryTransactionRequest(page, url);
19+
const transaction = eventData[2] as Event;
20+
21+
const rootSpanId = transaction?.contexts?.trace.spanId;
22+
23+
expect(transaction.spans).toHaveLength(3);
24+
25+
const span_1 = transaction.spans?.[0];
26+
expect(span_1?.op).toBe('span_1');
27+
expect(span_1?.parentSpanId).toEqual(rootSpanId);
28+
expect(span_1?.data).toMatchObject({ foo: 'bar', baz: [1, 2, 3] });
29+
30+
const span_3 = transaction.spans?.[1];
31+
expect(span_3?.op).toBe('span_3');
32+
expect(span_3?.parentSpanId).toEqual(rootSpanId);
33+
34+
const span_5 = transaction.spans?.[2];
35+
expect(span_5?.op).toBe('span_5');
36+
expect(span_5?.parentSpanId).toEqual(span_3?.spanId);
37+
});
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+
// eslint-disable-next-line no-unused-vars
3+
import * as _ from '@sentry/tracing';
4+
5+
window.Sentry = Sentry;
6+
7+
Sentry.init({
8+
dsn: 'https://[email protected]/1337',
9+
tracesSampleRate: 1.0,
10+
});
Lines changed: 11 additions & 0 deletions
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>

0 commit comments

Comments
 (0)