1
1
import { SentrySpan , getTraceData } from '../../../src/' ;
2
+ import * as SentryCoreCurrentScopes from '../../../src/currentScopes' ;
2
3
import * as SentryCoreTracing from '../../../src/tracing' ;
4
+ import * as SentryCoreSpanUtils from '../../../src/utils/spanUtils' ;
3
5
4
6
import { isValidBaggageString } from '../../../src/utils/traceData' ;
5
7
@@ -25,33 +27,38 @@ describe('getTraceData', () => {
25
27
jest . spyOn ( SentryCoreTracing , 'getDynamicSamplingContextFromSpan' ) . mockReturnValueOnce ( {
26
28
environment : 'production' ,
27
29
} ) ;
30
+ jest . spyOn ( SentryCoreSpanUtils , 'getActiveSpan' ) . mockImplementationOnce ( ( ) => mockedSpan ) ;
31
+ jest . spyOn ( SentryCoreCurrentScopes , 'getCurrentScope' ) . mockImplementationOnce ( ( ) => mockedScope ) ;
28
32
29
- const tags = getTraceData ( mockedSpan , mockedScope , mockedClient ) ;
33
+ const data = getTraceData ( ) ;
30
34
31
- expect ( tags ) . toEqual ( {
35
+ expect ( data ) . toEqual ( {
32
36
'sentry-trace' : '12345678901234567890123456789012-1234567890123456-1' ,
33
37
baggage : 'sentry-environment=production' ,
34
38
} ) ;
35
39
}
36
40
} ) ;
37
41
38
42
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 ,
54
58
) ;
59
+ jest . spyOn ( SentryCoreCurrentScopes , 'getClient' ) . mockImplementationOnce ( ( ) => mockedClient ) ;
60
+
61
+ const traceData = getTraceData ( ) ;
55
62
56
63
expect ( traceData ) . toEqual ( {
57
64
'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', () => {
65
72
public_key : undefined ,
66
73
} ) ;
67
74
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
+ } ;
79
84
} ,
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 ( ) ;
83
91
84
92
expect ( traceData ) . toEqual ( {
85
93
'sentry-trace' : '12345678901234567890123456789012-1234567890123456-1' ,
@@ -92,21 +100,21 @@ describe('getTraceData', () => {
92
100
public_key : undefined ,
93
101
} ) ;
94
102
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
+ } ;
106
112
} ,
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 ( ) ;
110
118
111
119
expect ( traceData ) . toEqual ( {
112
120
'sentry-trace' : '12345678901234567890123456789012-1234567890123456-1' ,
@@ -115,21 +123,19 @@ describe('getTraceData', () => {
115
123
} ) ;
116
124
117
125
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
+ } ;
129
135
} ,
130
- mockedScope ,
131
- mockedClient ,
132
- ) ;
136
+ } ) ) ;
137
+
138
+ const traceData = getTraceData ( ) ;
133
139
134
140
expect ( traceData ) . toEqual ( { } ) ;
135
141
} ) ;
0 commit comments