@@ -3,6 +3,7 @@ import type {
33 ClientOptions ,
44 Hub ,
55 Context ,
6+ Client ,
67 Transaction ,
78 TransactionMetadata
89} from '@sentry/types' ;
@@ -36,15 +37,22 @@ function makeTransactionMock(options = {}): Transaction {
3637 } as unknown as Transaction ;
3738}
3839
39- function makeHubMock ( { profilesSampleRate } : { profilesSampleRate : number | undefined } ) : Hub {
40+ function makeHubMock ( {
41+ profilesSampleRate,
42+ client
43+ } : {
44+ profilesSampleRate : number | undefined ;
45+ client ?: Partial < Client < ClientOptions < BaseTransportOptions > > > ;
46+ } ) : Hub {
4047 return {
4148 getClient : jest . fn ( ) . mockImplementation ( ( ) => {
4249 return {
4350 getOptions : jest . fn ( ) . mockImplementation ( ( ) => {
4451 return {
4552 profilesSampleRate
4653 } as unknown as ClientOptions < BaseTransportOptions > ;
47- } )
54+ } ) ,
55+ ...( client ?? { } )
4856 } ;
4957 } )
5058 } as unknown as Hub ;
@@ -131,4 +139,98 @@ describe('hubextensions', () => {
131139 expect ( startProfilingSpy ) . not . toHaveBeenCalledTimes ( 1 ) ;
132140 expect ( stopProfilingSpy ) . not . toHaveBeenCalledTimes ( 1 ) ;
133141 } ) ;
142+
143+ it ( 'disabled if neither profilesSampler and profilesSampleRate are not set' , ( ) => {
144+ const hub = makeHubMock ( { profilesSampleRate : undefined } ) ;
145+ const startTransaction = jest . fn ( ) . mockImplementation ( ( ) => makeTransactionMock ( ) ) ;
146+
147+ const maybeStartTransactionWithProfiling = __PRIVATE__wrapStartTransactionWithProfiling ( startTransaction ) ;
148+ const samplingContext = { beep : 'boop' } ;
149+ const transaction = maybeStartTransactionWithProfiling . call ( hub , { name : '' } , samplingContext ) ;
150+ transaction . finish ( ) ;
151+
152+ const startProfilingSpy = jest . spyOn ( profiler , 'startProfiling' ) ;
153+ expect ( startProfilingSpy ) . not . toHaveBeenCalled ( ) ;
154+ } ) ;
155+
156+ it ( 'does not call startProfiling if profilesSampler returns invalid rate' , ( ) => {
157+ const startProfilingSpy = jest . spyOn ( profiler , 'startProfiling' ) ;
158+ const options = { profilesSampler : jest . fn ( ) . mockReturnValue ( NaN ) } ;
159+ const hub = makeHubMock ( {
160+ profilesSampleRate : undefined ,
161+ client : {
162+ // @ts -expect-error mock this
163+ getOptions : ( ) => options
164+ }
165+ } ) ;
166+ const startTransaction = jest . fn ( ) . mockImplementation ( ( ) => makeTransactionMock ( ) ) ;
167+
168+ const maybeStartTransactionWithProfiling = __PRIVATE__wrapStartTransactionWithProfiling ( startTransaction ) ;
169+ const samplingContext = { beep : 'boop' } ;
170+ const transaction = maybeStartTransactionWithProfiling . call ( hub , { name : '' } , samplingContext ) ;
171+ transaction . finish ( ) ;
172+
173+ expect ( options . profilesSampler ) . toHaveBeenCalledWith ( samplingContext ) ;
174+ expect ( startProfilingSpy ) . not . toHaveBeenCalled ( ) ;
175+ } ) ;
176+
177+ it ( 'does not call startProfiling if profilesSampleRate is invalid' , ( ) => {
178+ const startProfilingSpy = jest . spyOn ( profiler , 'startProfiling' ) ;
179+ const options = { profilesSampler : jest . fn ( ) . mockReturnValue ( NaN ) } ;
180+ const hub = makeHubMock ( {
181+ profilesSampleRate : NaN ,
182+ client : {
183+ // @ts -expect-error mock this
184+ getOptions : ( ) => options
185+ }
186+ } ) ;
187+ const startTransaction = jest . fn ( ) . mockImplementation ( ( ) => makeTransactionMock ( ) ) ;
188+
189+ const maybeStartTransactionWithProfiling = __PRIVATE__wrapStartTransactionWithProfiling ( startTransaction ) ;
190+ const samplingContext = { beep : 'boop' } ;
191+ const transaction = maybeStartTransactionWithProfiling . call ( hub , { name : '' } , samplingContext ) ;
192+ transaction . finish ( ) ;
193+
194+ expect ( options . profilesSampler ) . toHaveBeenCalledWith ( samplingContext ) ;
195+ expect ( startProfilingSpy ) . not . toHaveBeenCalled ( ) ;
196+ } ) ;
197+
198+ it ( 'calls profilesSampler with sampling context' , ( ) => {
199+ const options = { profilesSampler : jest . fn ( ) } ;
200+ const hub = makeHubMock ( {
201+ profilesSampleRate : undefined ,
202+ client : {
203+ // @ts -expect-error mock this
204+ getOptions : ( ) => options
205+ }
206+ } ) ;
207+ const startTransaction = jest . fn ( ) . mockImplementation ( ( ) => makeTransactionMock ( ) ) ;
208+
209+ const maybeStartTransactionWithProfiling = __PRIVATE__wrapStartTransactionWithProfiling ( startTransaction ) ;
210+ const samplingContext = { beep : 'boop' } ;
211+ const transaction = maybeStartTransactionWithProfiling . call ( hub , { name : '' } , samplingContext ) ;
212+ transaction . finish ( ) ;
213+
214+ expect ( options . profilesSampler ) . toHaveBeenCalledWith ( samplingContext ) ;
215+ } ) ;
216+
217+ it ( 'prioritizes profilesSampler outcome over profilesSampleRate' , ( ) => {
218+ const startProfilingSpy = jest . spyOn ( profiler , 'startProfiling' ) ;
219+ const options = { profilesSampler : jest . fn ( ) . mockReturnValue ( 1 ) } ;
220+ const hub = makeHubMock ( {
221+ profilesSampleRate : 0 ,
222+ client : {
223+ // @ts -expect-error mock this
224+ getOptions : ( ) => options
225+ }
226+ } ) ;
227+ const startTransaction = jest . fn ( ) . mockImplementation ( ( ) => makeTransactionMock ( ) ) ;
228+
229+ const maybeStartTransactionWithProfiling = __PRIVATE__wrapStartTransactionWithProfiling ( startTransaction ) ;
230+ const samplingContext = { beep : 'boop' } ;
231+ const transaction = maybeStartTransactionWithProfiling . call ( hub , { name : '' } , samplingContext ) ;
232+ transaction . finish ( ) ;
233+
234+ expect ( startProfilingSpy ) . toHaveBeenCalled ( ) ;
235+ } ) ;
134236} ) ;
0 commit comments