Skip to content

Commit 63ab398

Browse files
committed
fix tests
1 parent b3988f5 commit 63ab398

File tree

2 files changed

+200
-26
lines changed

2 files changed

+200
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import type { Client } from '@sentry/types';
2+
import { Scope, SentrySpan, getCurrentScope, getGlobalScope, getIsolationScope, setCurrentClient } from '../../../src';
3+
import { freezeDscOnSpan } from '../../../src/tracing/dynamicSamplingContext';
4+
import { getSentryHeaders } from '../../../src/tracing/sentryHeaders';
5+
import type { TestClientOptions } from '../../mocks/client';
6+
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
7+
8+
const dsn = 'https://[email protected]/42';
9+
10+
const SCOPE_TRACE_ID = '12345678901234567890123456789012';
11+
const SCOPE_SPAN_ID = '1234567890123456';
12+
13+
function setupClient(opts?: TestClientOptions): Client {
14+
getCurrentScope().clear();
15+
getIsolationScope().clear();
16+
getGlobalScope().clear();
17+
18+
getCurrentScope().setPropagationContext({
19+
traceId: SCOPE_TRACE_ID,
20+
spanId: SCOPE_SPAN_ID,
21+
});
22+
23+
const options = getDefaultTestClientOptions({
24+
dsn,
25+
...opts,
26+
});
27+
const client = new TestClient(options);
28+
setCurrentClient(client);
29+
client.init();
30+
31+
return client;
32+
}
33+
34+
describe('getSentryHeaders', () => {
35+
beforeEach(() => {});
36+
37+
afterEach(() => {
38+
jest.clearAllMocks();
39+
});
40+
41+
it('works with a minimal client', () => {
42+
const client = setupClient();
43+
44+
const { sentryTrace, baggage } = getSentryHeaders({ client });
45+
46+
expect(sentryTrace).toEqual(`${SCOPE_TRACE_ID}-${SCOPE_SPAN_ID}`);
47+
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${SCOPE_TRACE_ID}`);
48+
});
49+
50+
it('allows to pass a specific scope', () => {
51+
const client = setupClient();
52+
53+
const traceId = '12345678901234567890123456789099';
54+
const spanId = '1234567890123499';
55+
const scope = new Scope();
56+
scope.setPropagationContext({
57+
traceId,
58+
spanId,
59+
});
60+
61+
const { sentryTrace, baggage } = getSentryHeaders({ client, scope });
62+
63+
expect(sentryTrace).toEqual(`${traceId}-${spanId}`);
64+
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
65+
});
66+
67+
it('works with a minimal unsampled span', () => {
68+
const client = setupClient();
69+
70+
const traceId = '12345678901234567890123456789099';
71+
const spanId = '1234567890123499';
72+
73+
const span = new SentrySpan({
74+
traceId,
75+
spanId,
76+
sampled: false,
77+
});
78+
79+
const { sentryTrace, baggage } = getSentryHeaders({ client, span });
80+
81+
expect(sentryTrace).toEqual(`${traceId}-${spanId}-0`);
82+
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
83+
});
84+
85+
it('works with a minimal sampled span', () => {
86+
const client = setupClient();
87+
88+
const traceId = '12345678901234567890123456789099';
89+
const spanId = '1234567890123499';
90+
91+
const span = new SentrySpan({
92+
traceId,
93+
spanId,
94+
sampled: true,
95+
});
96+
97+
const { sentryTrace, baggage } = getSentryHeaders({ client, span });
98+
99+
expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
100+
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
101+
});
102+
103+
it('works with a SentrySpan with frozen DSC', () => {
104+
const client = setupClient();
105+
106+
const traceId = '12345678901234567890123456789099';
107+
const spanId = '1234567890123499';
108+
109+
const span = new SentrySpan({
110+
traceId,
111+
spanId,
112+
sampled: true,
113+
});
114+
115+
freezeDscOnSpan(span, {
116+
environment: 'test-dev',
117+
public_key: '456',
118+
trace_id: '12345678901234567890123456789088',
119+
});
120+
121+
const { sentryTrace, baggage } = getSentryHeaders({ client, span });
122+
123+
expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
124+
expect(baggage).toEqual(
125+
'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088',
126+
);
127+
});
128+
129+
it('works with an OTEL span with frozen DSC in traceState', () => {
130+
const client = setupClient();
131+
132+
const traceId = '12345678901234567890123456789099';
133+
const spanId = '1234567890123499';
134+
135+
const span = new SentrySpan({
136+
traceId,
137+
spanId,
138+
sampled: true,
139+
});
140+
141+
span.spanContext = () => {
142+
const traceState = {
143+
set: () => traceState,
144+
unset: () => traceState,
145+
get: (key: string) => {
146+
if (key === 'sentry.dsc') {
147+
return 'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088';
148+
}
149+
return undefined;
150+
},
151+
serialize: () => '',
152+
};
153+
154+
return {
155+
traceId,
156+
spanId,
157+
sampled: true,
158+
traceFlags: 1,
159+
traceState,
160+
};
161+
};
162+
163+
const { sentryTrace, baggage } = getSentryHeaders({ client, span });
164+
165+
expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
166+
expect(baggage).toEqual(
167+
'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088',
168+
);
169+
});
170+
});

packages/core/test/lib/utils/traceData.test.ts

+30-26
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@ import { SentrySpan, getTraceData } from '../../../src/';
22
import * as SentryCoreCurrentScopes from '../../../src/currentScopes';
33
import * as SentryCoreExports from '../../../src/exports';
44
import * as SentryCoreTracing from '../../../src/tracing';
5+
import * as SentryCoreTracingDsc from '../../../src/tracing/dynamicSamplingContext';
6+
import { freezeDscOnSpan } from '../../../src/tracing/dynamicSamplingContext';
57
import * as SentryCoreSpanUtils from '../../../src/utils/spanUtils';
68

79
import { isValidBaggageString } from '../../../src/utils/traceData';
810

911
const TRACE_FLAG_SAMPLED = 1;
1012

11-
const mockedSpan = new SentrySpan({
12-
traceId: '12345678901234567890123456789012',
13-
spanId: '1234567890123456',
14-
sampled: true,
15-
});
16-
1713
const mockedClient = {} as any;
1814

1915
const mockedScope = {
2016
getPropagationContext: () => ({
2117
traceId: '123',
18+
spanId: '456',
2219
}),
2320
} as any;
2421

@@ -33,11 +30,16 @@ describe('getTraceData', () => {
3330

3431
it('returns the tracing data from the span, if a span is available', () => {
3532
{
36-
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
37-
environment: 'production',
33+
const mockedSpan = new SentrySpan({
34+
traceId: '12345678901234567890123456789012',
35+
spanId: '1234567890123456',
36+
sampled: true,
3837
});
39-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => mockedSpan);
40-
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
38+
freezeDscOnSpan(mockedSpan, { environment: 'production' });
39+
40+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => mockedSpan);
41+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
42+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => mockedClient);
4143

4244
const data = getTraceData();
4345

@@ -49,8 +51,8 @@ describe('getTraceData', () => {
4951
});
5052

5153
it('returns propagationContext DSC data if no span is available', () => {
52-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => undefined);
53-
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(
54+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => undefined);
55+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(
5456
() =>
5557
({
5658
getPropagationContext: () => ({
@@ -65,7 +67,7 @@ describe('getTraceData', () => {
6567
}),
6668
}) as any,
6769
);
68-
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
70+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => mockedClient);
6971

7072
const traceData = getTraceData();
7173

@@ -76,13 +78,13 @@ describe('getTraceData', () => {
7678
});
7779

7880
it('returns only the `sentry-trace` value if no DSC is available', () => {
79-
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
81+
jest.spyOn(SentryCoreTracingDsc, 'getDynamicSamplingContextFromSpan').mockReturnValue({
8082
trace_id: '',
8183
public_key: undefined,
8284
});
8385

8486
// @ts-expect-error - we don't need to provide all the properties
85-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
87+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
8688
isRecording: () => true,
8789
spanContext: () => {
8890
return {
@@ -93,8 +95,13 @@ describe('getTraceData', () => {
9395
},
9496
}));
9597

96-
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
97-
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
98+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
99+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => {
100+
return {
101+
getOptions: () => ({}),
102+
getDsn: () => ({}),
103+
} as any;
104+
});
98105

99106
const traceData = getTraceData();
100107

@@ -103,14 +110,14 @@ describe('getTraceData', () => {
103110
});
104111
});
105112

106-
it('returns only the `sentry-trace` tag if no DSC is available without a client', () => {
113+
it('returns empty object without a client', () => {
107114
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
108115
trace_id: '',
109116
public_key: undefined,
110117
});
111118

112119
// @ts-expect-error - we don't need to provide all the properties
113-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
120+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
114121
isRecording: () => true,
115122
spanContext: () => {
116123
return {
@@ -120,20 +127,17 @@ describe('getTraceData', () => {
120127
};
121128
},
122129
}));
123-
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
124-
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => undefined);
130+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
131+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => undefined);
125132

126133
const traceData = getTraceData();
127134

128-
expect(traceData).toEqual({
129-
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
130-
});
131-
expect('baggage' in traceData).toBe(false);
135+
expect(traceData).toEqual({});
132136
});
133137

134138
it('returns an empty object if the `sentry-trace` value is invalid', () => {
135139
// @ts-expect-error - we don't need to provide all the properties
136-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
140+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
137141
isRecording: () => true,
138142
spanContext: () => {
139143
return {

0 commit comments

Comments
 (0)