1
+ /* eslint-disable @typescript-eslint/unbound-method */
1
2
import { BrowserClient } from '@sentry/browser' ;
2
3
import * as hubModuleRaw from '@sentry/hub' ; // for mocking
3
4
import { getMainCarrier , Hub } from '@sentry/hub' ;
@@ -15,7 +16,7 @@ import { addExtensionMethods } from '../src/hubextensions';
15
16
// (This doesn't affect the utils module because it uses `export * from './myModule' syntax rather than `export
16
17
// {<individually named methods>} from './myModule'` syntax in its index.ts. Only *named* exports seem to trigger the
17
18
// problem.)
18
- const hubModule = { ...hubModuleRaw }
19
+ const hubModule = { ...hubModuleRaw } ;
19
20
20
21
addExtensionMethods ( ) ;
21
22
@@ -36,29 +37,26 @@ describe('Hub', () => {
36
37
} ) ;
37
38
38
39
describe ( 'getTransaction()' , ( ) => {
39
-
40
40
it ( 'should find a transaction which has been set on the scope' , ( ) => {
41
41
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
42
42
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
43
43
hub . configureScope ( scope => {
44
44
scope . setSpan ( transaction ) ;
45
45
} ) ;
46
46
47
- expect ( hub . getScope ( ) ?. getTransaction ( ) ) . toBe ( transaction )
48
-
47
+ expect ( hub . getScope ( ) ?. getTransaction ( ) ) . toBe ( transaction ) ;
49
48
} ) ;
50
49
51
50
it ( "should not find an open transaction if it's not on the scope" , ( ) => {
52
51
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 1 } ) ) ;
53
52
hub . startTransaction ( { name : 'dogpark' } ) ;
54
53
55
- expect ( hub . getScope ( ) ?. getTransaction ( ) ) . toBeUndefined ( )
54
+ expect ( hub . getScope ( ) ?. getTransaction ( ) ) . toBeUndefined ( ) ;
56
55
} ) ;
57
56
} ) ; // end describe('getTransaction()')
58
57
59
58
describe ( 'transaction sampling' , ( ) => {
60
59
describe ( 'options' , ( ) => {
61
-
62
60
it ( "should call tracesSampler if it's defined" , ( ) => {
63
61
const tracesSampler = jest . fn ( ) ;
64
62
const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
@@ -74,11 +72,9 @@ describe('Hub', () => {
74
72
75
73
expect ( tracesSampler ) . toHaveBeenCalled ( ) ;
76
74
} ) ;
77
-
78
75
} ) ; // end describe('options')
79
76
80
77
describe ( 'default sample context' , ( ) => {
81
-
82
78
it ( 'should extract request data for default sampling context when in node' , ( ) => {
83
79
// make sure we look like we're in node
84
80
( isNodeEnv as jest . Mock ) . mockReturnValue ( true ) ;
@@ -115,15 +111,17 @@ describe('Hub', () => {
115
111
hub . startTransaction ( { name : 'dogpark' } ) ;
116
112
117
113
// post-normalization request object
118
- expect ( tracesSampler ) . toHaveBeenCalledWith ( expect . objectContaining ( {
119
- request : {
120
- headers : { ears : 'furry' , nose : 'wet' , tongue : 'panting' , cookie : 'favorite=zukes' } ,
121
- method : 'wagging' ,
122
- url : 'http://the.dog.park/by/the/trees/?chase=me&please=thankyou' ,
123
- cookies : { favorite : 'zukes' } ,
124
- query_string : 'chase=me&please=thankyou' ,
125
- } ,
126
- } ) ) ;
114
+ expect ( tracesSampler ) . toHaveBeenCalledWith (
115
+ expect . objectContaining ( {
116
+ request : {
117
+ headers : { ears : 'furry' , nose : 'wet' , tongue : 'panting' , cookie : 'favorite=zukes' } ,
118
+ method : 'wagging' ,
119
+ url : 'http://the.dog.park/by/the/trees/?chase=me&please=thankyou' ,
120
+ cookies : { favorite : 'zukes' } ,
121
+ query_string : 'chase=me&please=thankyou' ,
122
+ } ,
123
+ } ) ,
124
+ ) ;
127
125
} ) ;
128
126
129
127
it ( 'should extract window.location/self.location for default sampling context when in browser/service worker' , ( ) => {
@@ -153,7 +151,6 @@ describe('Hub', () => {
153
151
} ) ; // end describe('defaultSampleContext')
154
152
155
153
describe ( 'while sampling' , ( ) => {
156
-
157
154
it ( 'should not sample transactions when tracing is disabled' , ( ) => {
158
155
// neither tracesSampleRate nor tracesSampler is defined -> tracing disabled
159
156
const hub = new Hub ( new BrowserClient ( { } ) ) ;
@@ -176,51 +173,51 @@ describe('Hub', () => {
176
173
expect ( transaction . sampled ) . toBe ( true ) ;
177
174
} ) ;
178
175
179
- it ( "should reject tracesSampleRates which aren't numbers" , ( ) => {
180
- const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 'dogs!' as any } ) ) ;
181
- hub . startTransaction ( { name : 'dogpark' } ) ;
176
+ it ( "should reject tracesSampleRates which aren't numbers" , ( ) => {
177
+ const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 'dogs!' as any } ) ) ;
178
+ hub . startTransaction ( { name : 'dogpark' } ) ;
182
179
183
- expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be a number' ) ) ;
184
- } ) ;
180
+ expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be a number' ) ) ;
181
+ } ) ;
185
182
186
- it ( 'should reject tracesSampleRates less than 0' , ( ) => {
187
- const hub = new Hub ( new BrowserClient ( { tracesSampleRate : - 26 } ) ) ;
188
- hub . startTransaction ( { name : 'dogpark' } ) ;
183
+ it ( 'should reject tracesSampleRates less than 0' , ( ) => {
184
+ const hub = new Hub ( new BrowserClient ( { tracesSampleRate : - 26 } ) ) ;
185
+ hub . startTransaction ( { name : 'dogpark' } ) ;
189
186
190
- expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
191
- } ) ;
187
+ expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
188
+ } ) ;
192
189
193
- it ( 'should reject tracesSampleRates greater than 1' , ( ) => {
194
- const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 26 } ) ) ;
195
- hub . startTransaction ( { name : 'dogpark' } ) ;
190
+ it ( 'should reject tracesSampleRates greater than 1' , ( ) => {
191
+ const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 26 } ) ) ;
192
+ hub . startTransaction ( { name : 'dogpark' } ) ;
196
193
197
- expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
198
- } ) ;
194
+ expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
195
+ } ) ;
199
196
200
- it ( "should reject tracesSampler return values which aren't numbers" , ( ) => {
201
- const tracesSampler = jest . fn ( ) . mockReturnValue ( " dogs!" )
202
- const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
203
- hub . startTransaction ( { name : 'dogpark' } ) ;
197
+ it ( "should reject tracesSampler return values which aren't numbers" , ( ) => {
198
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( ' dogs!' ) ;
199
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
200
+ hub . startTransaction ( { name : 'dogpark' } ) ;
204
201
205
- expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be a number' ) ) ;
206
- } ) ;
202
+ expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be a number' ) ) ;
203
+ } ) ;
207
204
208
- it ( 'should reject tracesSampler return values less than 0' , ( ) => {
209
- const tracesSampler = jest . fn ( ) . mockReturnValue ( - 12 )
210
- const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
211
- hub . startTransaction ( { name : 'dogpark' } ) ;
205
+ it ( 'should reject tracesSampler return values less than 0' , ( ) => {
206
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( - 12 ) ;
207
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
208
+ hub . startTransaction ( { name : 'dogpark' } ) ;
212
209
213
- expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
214
- } ) ;
210
+ expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
211
+ } ) ;
215
212
216
- it ( 'should reject tracesSampler return values greater than 1' , ( ) => {
217
- const tracesSampler = jest . fn ( ) . mockReturnValue ( 31 )
218
- const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
219
- hub . startTransaction ( { name : 'dogpark' } ) ;
213
+ it ( 'should reject tracesSampler return values greater than 1' , ( ) => {
214
+ const tracesSampler = jest . fn ( ) . mockReturnValue ( 31 ) ;
215
+ const hub = new Hub ( new BrowserClient ( { tracesSampler } ) ) ;
216
+ hub . startTransaction ( { name : 'dogpark' } ) ;
220
217
221
- expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
222
- } ) ;
223
- } ) ; // end describe('while sampling')
218
+ expect ( logger . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Sample rate must be between 0 and 1' ) ) ;
219
+ } ) ;
220
+ } ) ; // end describe('while sampling')
224
221
225
222
it ( 'should propagate sampling decision to child spans' , ( ) => {
226
223
const hub = new Hub ( new BrowserClient ( { tracesSampleRate : 0 } ) ) ;
@@ -231,18 +228,18 @@ describe('Hub', () => {
231
228
} ) ;
232
229
233
230
it ( 'should drop transactions with sampled = false' , ( ) => {
234
- const client = new BrowserClient ( { tracesSampleRate : 0 } )
235
- jest . spyOn ( client , 'captureEvent' )
231
+ const client = new BrowserClient ( { tracesSampleRate : 0 } ) ;
232
+ jest . spyOn ( client , 'captureEvent' ) ;
236
233
237
234
const hub = new Hub ( client ) ;
238
235
const transaction = hub . startTransaction ( { name : 'dogpark' } ) ;
239
236
240
- jest . spyOn ( transaction , 'finish' )
241
- transaction . finish ( )
237
+ jest . spyOn ( transaction , 'finish' ) ;
238
+ transaction . finish ( ) ;
242
239
243
240
expect ( transaction . sampled ) . toBe ( false ) ;
244
241
expect ( transaction . finish ) . toReturnWith ( undefined ) ;
245
- expect ( client . captureEvent ) . not . toBeCalled ( )
242
+ expect ( client . captureEvent ) . not . toBeCalled ( ) ;
246
243
} ) ;
247
244
} ) ; // end describe('transaction sampling')
248
245
} ) ; // end describe('Hub')
0 commit comments