Repository navigation
Expand file tree
/
Copy pathapi.test.ts
More file actions
104 lines (84 loc) · 3.07 KB
/
Copy pathapi.test.ts
File metadata and controls
104 lines (84 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import type { Event } from '@sentry/core';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { getActiveSpan, getClient, startInactiveSpan, startSpan, withActiveSpan } from '../../src';
import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit';
afterEach(() => {
vi.restoreAllMocks();
cleanupOtel();
});
describe('withActiveSpan()', () => {
it('should set the active span within the callback', () => {
mockSdkInit();
const inactiveSpan = startInactiveSpan({ name: 'inactive-span' });
expect(getActiveSpan()).not.toBe(inactiveSpan);
withActiveSpan(inactiveSpan, () => {
expect(getActiveSpan()).toBe(inactiveSpan);
});
});
it('should create child spans when calling startSpan within the callback', async () => {
const beforeSendTransaction = vi.fn(() => null);
mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction });
const client = getClient();
const inactiveSpan = startInactiveSpan({ name: 'inactive-span' });
withActiveSpan(inactiveSpan, () => {
startSpan({ name: 'child-span' }, () => {});
});
startSpan({ name: 'floating-span' }, () => {});
inactiveSpan.end();
await client?.flush();
// The child span should be a child of the inactive span
expect(beforeSendTransaction).toHaveBeenCalledWith(
expect.objectContaining({
transaction: 'inactive-span',
spans: expect.arrayContaining([expect.any(Object)]),
}),
expect.anything(),
);
// The floating span should be a separate transaction
expect(beforeSendTransaction).toHaveBeenCalledWith(
expect.objectContaining({
transaction: 'floating-span',
}),
expect.anything(),
);
});
it('when `null` is passed, no span should be active within the callback', () => {
expect.assertions(1);
startSpan({ name: 'parent-span' }, () => {
withActiveSpan(null, () => {
expect(getActiveSpan()).toBeUndefined();
});
});
});
it('when `null` is passed, should start a new trace for new spans', async () => {
const transactions: Event[] = [];
const beforeSendTransaction = vi.fn((event: Event) => {
transactions.push(event);
return null;
});
mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction });
const client = getClient();
startSpan({ name: 'parent-span' }, () => {
withActiveSpan(null, () => {
startSpan({ name: 'child-span' }, () => {});
});
});
await client?.flush();
expect(beforeSendTransaction).toHaveBeenCalledTimes(2);
// The child span should be a child of the inactive span
expect(beforeSendTransaction).toHaveBeenCalledWith(
expect.objectContaining({
transaction: 'parent-span',
spans: expect.not.arrayContaining([expect.objectContaining({ description: 'child-span' })]),
}),
expect.anything(),
);
// The floating span should be a separate transaction
expect(beforeSendTransaction).toHaveBeenCalledWith(
expect.objectContaining({
transaction: 'child-span',
}),
expect.anything(),
);
});
});