11import { SentrySpan , getTraceData } from '../../../src/' ;
2+ import * as SentryCoreCurrentScopes from '../../../src/currentScopes' ;
23import * as SentryCoreTracing from '../../../src/tracing' ;
4+ import * as SentryCoreSpanUtils from '../../../src/utils/spanUtils' ;
35
46import { isValidBaggageString } from '../../../src/utils/traceData' ;
57
@@ -25,33 +27,38 @@ describe('getTraceData', () => {
2527 jest . spyOn ( SentryCoreTracing , 'getDynamicSamplingContextFromSpan' ) . mockReturnValueOnce ( {
2628 environment : 'production' ,
2729 } ) ;
30+ jest . spyOn ( SentryCoreSpanUtils , 'getActiveSpan' ) . mockImplementationOnce ( ( ) => mockedSpan ) ;
31+ jest . spyOn ( SentryCoreCurrentScopes , 'getCurrentScope' ) . mockImplementationOnce ( ( ) => mockedScope ) ;
2832
29- const tags = getTraceData ( mockedSpan , mockedScope , mockedClient ) ;
33+ const data = getTraceData ( ) ;
3034
31- expect ( tags ) . toEqual ( {
35+ expect ( data ) . toEqual ( {
3236 'sentry-trace' : '12345678901234567890123456789012-1234567890123456-1' ,
3337 baggage : 'sentry-environment=production' ,
3438 } ) ;
3539 }
3640 } ) ;
3741
3842 it ( 'returns propagationContext DSC data if no span is available' , ( ) => {
39- const traceData = getTraceData (
40- undefined ,
41- {
42- getPropagationContext : ( ) => ( {
43- traceId : '12345678901234567890123456789012' ,
44- sampled : true ,
45- spanId : '1234567890123456' ,
46- dsc : {
47- environment : 'staging' ,
48- public_key : 'key ' ,
49- trace_id : '12345678901234567890123456789012 ' ,
50- } ,
51- } ) ,
52- } as any ,
53- mockedClient ,
43+ jest . spyOn ( SentryCoreSpanUtils , 'getActiveSpan' ) . mockImplementationOnce ( ( ) => undefined ) ;
44+ jest . spyOn ( SentryCoreCurrentScopes , 'getCurrentScope' ) . mockImplementationOnce (
45+ ( ) =>
46+ ( {
47+ getPropagationContext : ( ) => ( {
48+ traceId : '12345678901234567890123456789012' ,
49+ sampled : true ,
50+ spanId : '1234567890123456' ,
51+ dsc : {
52+ environment : 'staging ' ,
53+ public_key : 'key ' ,
54+ trace_id : '12345678901234567890123456789012' ,
55+ } ,
56+ } ) ,
57+ } ) as any ,
5458 ) ;
59+ jest . spyOn ( SentryCoreCurrentScopes , 'getClient' ) . mockImplementationOnce ( ( ) => mockedClient ) ;
60+
61+ const traceData = getTraceData ( ) ;
5562
5663 expect ( traceData ) . toEqual ( {
5764 'sentry-trace' : expect . stringMatching ( / 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 - ( .{ 16 } ) - 1 / ) ,
@@ -65,21 +72,22 @@ describe('getTraceData', () => {
6572 public_key : undefined ,
6673 } ) ;
6774
68- const traceData = getTraceData (
69- // @ts -expect-error - we don't need to provide all the properties
70- {
71- isRecording : ( ) => true ,
72- spanContext : ( ) => {
73- return {
74- traceId : '12345678901234567890123456789012' ,
75- spanId : '1234567890123456' ,
76- traceFlags : TRACE_FLAG_SAMPLED ,
77- } ;
78- } ,
75+ // @ts -expect-error - we don't need to provide all the properties
76+ jest . spyOn ( SentryCoreSpanUtils , 'getActiveSpan' ) . mockImplementationOnce ( ( ) => ( {
77+ isRecording : ( ) => true ,
78+ spanContext : ( ) => {
79+ return {
80+ traceId : '12345678901234567890123456789012' ,
81+ spanId : '1234567890123456' ,
82+ traceFlags : TRACE_FLAG_SAMPLED ,
83+ } ;
7984 } ,
80- mockedScope ,
81- mockedClient ,
82- ) ;
85+ } ) ) ;
86+
87+ jest . spyOn ( SentryCoreCurrentScopes , 'getCurrentScope' ) . mockImplementationOnce ( ( ) => mockedScope ) ;
88+ jest . spyOn ( SentryCoreCurrentScopes , 'getClient' ) . mockImplementationOnce ( ( ) => mockedClient ) ;
89+
90+ const traceData = getTraceData ( ) ;
8391
8492 expect ( traceData ) . toEqual ( {
8593 'sentry-trace' : '12345678901234567890123456789012-1234567890123456-1' ,
@@ -92,21 +100,21 @@ describe('getTraceData', () => {
92100 public_key : undefined ,
93101 } ) ;
94102
95- const traceData = getTraceData (
96- // @ts -expect-error - we don't need to provide all the properties
97- {
98- isRecording : ( ) => true ,
99- spanContext : ( ) => {
100- return {
101- traceId : '12345678901234567890123456789012' ,
102- spanId : '1234567890123456' ,
103- traceFlags : TRACE_FLAG_SAMPLED ,
104- } ;
105- } ,
103+ // @ts -expect-error - we don't need to provide all the properties
104+ jest . spyOn ( SentryCoreSpanUtils , 'getActiveSpan' ) . mockImplementationOnce ( ( ) => ( {
105+ isRecording : ( ) => true ,
106+ spanContext : ( ) => {
107+ return {
108+ traceId : '12345678901234567890123456789012' ,
109+ spanId : '1234567890123456' ,
110+ traceFlags : TRACE_FLAG_SAMPLED ,
111+ } ;
106112 } ,
107- mockedScope ,
108- undefined ,
109- ) ;
113+ } ) ) ;
114+ jest . spyOn ( SentryCoreCurrentScopes , 'getCurrentScope' ) . mockImplementationOnce ( ( ) => mockedScope ) ;
115+ jest . spyOn ( SentryCoreCurrentScopes , 'getClient' ) . mockImplementationOnce ( ( ) => undefined ) ;
116+
117+ const traceData = getTraceData ( ) ;
110118
111119 expect ( traceData ) . toEqual ( {
112120 'sentry-trace' : '12345678901234567890123456789012-1234567890123456-1' ,
@@ -115,21 +123,19 @@ describe('getTraceData', () => {
115123 } ) ;
116124
117125 it ( 'returns an empty object if the `sentry-trace` value is invalid' , ( ) => {
118- const traceData = getTraceData (
119- // @ts -expect-error - we don't need to provide all the properties
120- {
121- isRecording : ( ) => true ,
122- spanContext : ( ) => {
123- return {
124- traceId : '1234567890123456789012345678901+' ,
125- spanId : '1234567890123456' ,
126- traceFlags : TRACE_FLAG_SAMPLED ,
127- } ;
128- } ,
126+ // @ts -expect-error - we don't need to provide all the properties
127+ jest . spyOn ( SentryCoreSpanUtils , 'getActiveSpan' ) . mockImplementationOnce ( ( ) => ( {
128+ isRecording : ( ) => true ,
129+ spanContext : ( ) => {
130+ return {
131+ traceId : '1234567890123456789012345678901+' ,
132+ spanId : '1234567890123456' ,
133+ traceFlags : TRACE_FLAG_SAMPLED ,
134+ } ;
129135 } ,
130- mockedScope ,
131- mockedClient ,
132- ) ;
136+ } ) ) ;
137+
138+ const traceData = getTraceData ( ) ;
133139
134140 expect ( traceData ) . toEqual ( { } ) ;
135141 } ) ;
0 commit comments