@@ -33,6 +33,8 @@ describe('Hub', () => {
3333    it ( 'should find a transaction which has been set on the scope if sampled = true' ,  ( )  =>  { 
3434      const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 1  } ) ) ; 
3535      const  transaction  =  hub . startTransaction ( {  name : 'dogpark'  } ) ; 
36+       transaction . sampled  =  true ; 
37+ 
3638      hub . configureScope ( scope  =>  { 
3739        scope . setSpan ( transaction ) ; 
3840      } ) ; 
@@ -42,7 +44,9 @@ describe('Hub', () => {
4244
4345    it ( 'should find a transaction which has been set on the scope if sampled = false' ,  ( )  =>  { 
4446      const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 1  } ) ) ; 
45-       const  transaction  =  hub . startTransaction ( {  name : 'dogpark' ,  sampled : false  } ) ; 
47+       const  transaction  =  hub . startTransaction ( {  name : 'dogpark'  } ) ; 
48+       transaction . sampled  =  false ; 
49+ 
4650      hub . configureScope ( scope  =>  { 
4751        scope . setSpan ( transaction ) ; 
4852      } ) ; 
@@ -59,33 +63,6 @@ describe('Hub', () => {
5963  } ) ; 
6064
6165  describe ( 'transaction sampling' ,  ( )  =>  { 
62-     describe ( 'tracesSampleRate and tracesSampler options' ,  ( )  =>  { 
63-       it ( "should call tracesSampler if it's defined" ,  ( )  =>  { 
64-         const  tracesSampler  =  jest . fn ( ) ; 
65-         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
66-         hub . startTransaction ( {  name : 'dogpark'  } ) ; 
67- 
68-         expect ( tracesSampler ) . toHaveBeenCalled ( ) ; 
69-       } ) ; 
70- 
71-       it ( 'should prefer tracesSampler to tracesSampleRate' ,  ( )  =>  { 
72-         const  tracesSampler  =  jest . fn ( ) ; 
73-         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 1 ,  tracesSampler } ) ) ; 
74-         hub . startTransaction ( {  name : 'dogpark'  } ) ; 
75- 
76-         expect ( tracesSampler ) . toHaveBeenCalled ( ) ; 
77-       } ) ; 
78- 
79-       it ( 'tolerates tracesSampler returning a boolean' ,  ( )  =>  { 
80-         const  tracesSampler  =  jest . fn ( ) . mockReturnValue ( true ) ; 
81-         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
82-         const  transaction  =  hub . startTransaction ( {  name : 'dogpark'  } ) ; 
83- 
84-         expect ( tracesSampler ) . toHaveBeenCalled ( ) ; 
85-         expect ( transaction . sampled ) . toBe ( true ) ; 
86-       } ) ; 
87-     } ) ; 
88- 
8966    describe ( 'default sample context' ,  ( )  =>  { 
9067      it ( 'should extract request data for default sampling context when in node' ,  ( )  =>  { 
9168        // make sure we look like we're in node 
@@ -201,25 +178,78 @@ describe('Hub', () => {
201178        expect ( transaction . sampled ) . toBe ( false ) ; 
202179      } ) ; 
203180
204-       it ( 'should not try to override sampling decision provided in transaction context' ,  ( )  =>  { 
205-         // setting tracesSampleRate to 1 means that without the override, the sampling decision should be true 
181+       it ( 'should set sampled = false if tracesSampleRate is 0' ,  ( )  =>  { 
182+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 0  } ) ) ; 
183+         const  transaction  =  hub . startTransaction ( {  name : 'dogpark'  } ) ; 
184+ 
185+         expect ( transaction . sampled ) . toBe ( false ) ; 
186+       } ) ; 
187+ 
188+       it ( 'should set sampled = true if tracesSampleRate is 1' ,  ( )  =>  { 
206189        const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 1  } ) ) ; 
207-         const  transaction  =  hub . startTransaction ( {  name : 'dogpark' ,   sampled :  false  } ) ; 
190+         const  transaction  =  hub . startTransaction ( {  name : 'dogpark'  } ) ; 
208191
192+         expect ( transaction . sampled ) . toBe ( true ) ; 
193+       } ) ; 
194+ 
195+       it ( "should call tracesSampler if it's defined" ,  ( )  =>  { 
196+         const  tracesSampler  =  jest . fn ( ) ; 
197+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
198+         hub . startTransaction ( {  name : 'dogpark'  } ) ; 
199+ 
200+         expect ( tracesSampler ) . toHaveBeenCalled ( ) ; 
201+       } ) ; 
202+ 
203+       it ( 'should set sampled = false if tracesSampler returns 0' ,  ( )  =>  { 
204+         const  tracesSampler  =  jest . fn ( ) . mockReturnValue ( 0 ) ; 
205+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
206+         const  transaction  =  hub . startTransaction ( {  name : 'dogpark'  } ) ; 
207+ 
208+         expect ( tracesSampler ) . toHaveBeenCalled ( ) ; 
209209        expect ( transaction . sampled ) . toBe ( false ) ; 
210210      } ) ; 
211211
212-       it ( 'should not sample transactions when tracesSampleRate is 0' ,  ( )  =>  { 
213-         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 0  } ) ) ; 
212+       it ( 'should set sampled = true if tracesSampler returns 1' ,  ( )  =>  { 
213+         const  tracesSampler  =  jest . fn ( ) . mockReturnValue ( 1 ) ; 
214+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
214215        const  transaction  =  hub . startTransaction ( {  name : 'dogpark'  } ) ; 
215216
217+         expect ( tracesSampler ) . toHaveBeenCalled ( ) ; 
218+         expect ( transaction . sampled ) . toBe ( true ) ; 
219+       } ) ; 
220+ 
221+       it ( 'should not try to override positive sampling decision provided in transaction context' ,  ( )  =>  { 
222+         // so that the decision otherwise would be false 
223+         const  tracesSampler  =  jest . fn ( ) . mockReturnValue ( 0 ) ; 
224+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
225+         const  transaction  =  hub . startTransaction ( {  name : 'dogpark' ,  sampled : true  } ) ; 
226+ 
227+         expect ( transaction . sampled ) . toBe ( true ) ; 
228+       } ) ; 
229+ 
230+       it ( 'should not try to override negative sampling decision provided in transaction context' ,  ( )  =>  { 
231+         // so that the decision otherwise would be true 
232+         const  tracesSampler  =  jest . fn ( ) . mockReturnValue ( 1 ) ; 
233+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
234+         const  transaction  =  hub . startTransaction ( {  name : 'dogpark' ,  sampled : false  } ) ; 
235+ 
216236        expect ( transaction . sampled ) . toBe ( false ) ; 
217237      } ) ; 
218238
219-       it ( 'should sample transactions when tracesSampleRate is 1' ,  ( )  =>  { 
220-         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 1  } ) ) ; 
239+       it ( 'should prefer tracesSampler to tracesSampleRate' ,  ( )  =>  { 
240+         const  tracesSampler  =  jest . fn ( ) ; 
241+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 1 ,  tracesSampler } ) ) ; 
242+         hub . startTransaction ( {  name : 'dogpark'  } ) ; 
243+ 
244+         expect ( tracesSampler ) . toHaveBeenCalled ( ) ; 
245+       } ) ; 
246+ 
247+       it ( 'should tolerate tracesSampler returning a boolean' ,  ( )  =>  { 
248+         const  tracesSampler  =  jest . fn ( ) . mockReturnValue ( true ) ; 
249+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
221250        const  transaction  =  hub . startTransaction ( {  name : 'dogpark'  } ) ; 
222251
252+         expect ( tracesSampler ) . toHaveBeenCalled ( ) ; 
223253        expect ( transaction . sampled ) . toBe ( true ) ; 
224254      } ) ; 
225255    } ) ; 
@@ -386,11 +416,21 @@ describe('Hub', () => {
386416        expect ( extractTraceparentData ( headers [ 'sentry-trace' ] ) ! . parentSampled ) . toBe ( false ) ; 
387417      } ) ; 
388418
389-       it ( 'should propagate sampling decision to child transactions in fetch header' ,  ( )  =>  { 
419+       it ( 'should propagate positive sampling decision to child transactions in fetch header' ,  ( )  =>  { 
420+         // TODO (kmclb) 
421+       } ) ; 
422+ 
423+       it ( 'should propagate negative sampling decision to child transactions in fetch header' ,  ( )  =>  { 
390424        // TODO (kmclb) 
391425      } ) ; 
392426
393-       it ( "should inherit parent's sampling decision when creating a new transaction if tracesSampler is undefined" ,  ( )  =>  { 
427+       it ( "should inherit parent's positive sampling decision if tracesSampler is undefined" ,  ( )  =>  { 
428+         // This is untestable, because there's no way to guarantee that a non-inherited decision is false (thus proving 
429+         // the parent's positive decision takes precedence) without using tracesSampler, since setting tracesSampleRate 
430+         // to 0 here will disable tracing all together. See below for the opposite test, though. 
431+       } ) ; 
432+ 
433+       it ( "should inherit parent's negative sampling decision if tracesSampler is undefined" ,  ( )  =>  { 
394434        // tracesSampleRate = 1 means every transaction should end up with sampled = true, so make parent's decision the 
395435        // opposite to prove that inheritance takes precedence over tracesSampleRate 
396436        const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampleRate : 1  } ) ) ; 
@@ -405,7 +445,7 @@ describe('Hub', () => {
405445        expect ( transaction . sampled ) . toBe ( parentSamplingDecsion ) ; 
406446      } ) ; 
407447
408-       it ( "should ignore parent's sampling decision when tracesSampler is defined" ,  ( )  =>  { 
448+       it ( "should ignore parent's positive  sampling decision when tracesSampler is defined" ,  ( )  =>  { 
409449        // this tracesSampler causes every transaction to end up with sampled = true, so make parent's decision the 
410450        // opposite to prove that tracesSampler takes precedence over inheritance 
411451        const  tracesSampler  =  ( )  =>  true ; 
@@ -421,6 +461,23 @@ describe('Hub', () => {
421461
422462        expect ( transaction . sampled ) . not . toBe ( parentSamplingDecsion ) ; 
423463      } ) ; 
464+ 
465+       it ( "should ignore parent's negative sampling decision when tracesSampler is defined" ,  ( )  =>  { 
466+         // this tracesSampler causes every transaction to end up with sampled = false, so make parent's decision the 
467+         // opposite to prove that tracesSampler takes precedence over inheritance 
468+         const  tracesSampler  =  ( )  =>  false ; 
469+         const  parentSamplingDecsion  =  true ; 
470+ 
471+         const  hub  =  new  Hub ( new  BrowserClient ( {  tracesSampler } ) ) ; 
472+ 
473+         const  transaction  =  hub . startTransaction ( { 
474+           name : 'dogpark' , 
475+           parentSpanId : '12312012' , 
476+           parentSampled : parentSamplingDecsion , 
477+         } ) ; 
478+ 
479+         expect ( transaction . sampled ) . not . toBe ( parentSamplingDecsion ) ; 
480+       } ) ; 
424481    } ) ; 
425482  } ) ; 
426483} ) ; 
0 commit comments