@@ -7,8 +7,9 @@ import type { Envelope, Event, Profile, Transaction, Transport } from '@sentry/t
7
7
8
8
import * as Sentry from '../../src/js' ;
9
9
import { HermesProfiling } from '../../src/js/integrations' ;
10
+ import type { NativeDeviceContextsResponse } from '../../src/js/NativeRNSentry' ;
10
11
import type * as Hermes from '../../src/js/profiling/hermes' ;
11
- import { isHermesEnabled } from '../../src/js/utils/environment' ;
12
+ import { getDefaultEnvironment , isHermesEnabled } from '../../src/js/utils/environment' ;
12
13
import { RN_GLOBAL_OBJ } from '../../src/js/utils/worldwide' ;
13
14
import { MOCK_DSN } from '../mockDsn' ;
14
15
import { envelopeItemPayload , envelopeItems } from '../testutils' ;
@@ -35,7 +36,7 @@ describe('profiling integration', () => {
35
36
} ) ;
36
37
37
38
test ( 'should start profile if there is a transaction running when integration is created' , ( ) => {
38
- mock = initTestClient ( false ) ;
39
+ mock = initTestClient ( { withProfiling : false } ) ;
39
40
jest . runAllTimers ( ) ;
40
41
jest . clearAllMocks ( ) ;
41
42
@@ -65,6 +66,96 @@ describe('profiling integration', () => {
65
66
] ) ;
66
67
} ) ;
67
68
69
+ describe ( 'environment' , ( ) => {
70
+ beforeEach ( ( ) => {
71
+ ( getDefaultEnvironment as jest . Mock ) . mockReturnValue ( 'mocked' ) ;
72
+ mockWrapper . NATIVE . fetchNativeDeviceContexts . mockResolvedValue ( < NativeDeviceContextsResponse > {
73
+ environment : 'native' ,
74
+ } ) ;
75
+ } ) ;
76
+
77
+ const expectTransactionWithEnvironment = ( envelope : Envelope | undefined , env : string | undefined ) => {
78
+ const transactionEvent = envelope ?. [ envelopeItems ] [ 0 ] [ envelopeItemPayload ] as Event ;
79
+ expect ( transactionEvent ) . toEqual (
80
+ expect . objectContaining < Partial < Event > > ( {
81
+ environment : env ,
82
+ } ) ,
83
+ ) ;
84
+ } ;
85
+
86
+ const expectProfileWithEnvironment = ( envelope : Envelope | undefined , env : string | undefined ) => {
87
+ const profileEvent = ( envelope ?. [ envelopeItems ] [ 1 ] as [ { type : 'profile' } , Profile ] ) [ 1 ] ;
88
+ expect ( profileEvent ) . toEqual (
89
+ expect . objectContaining < Partial < Profile > > ( {
90
+ environment : env ,
91
+ } ) ,
92
+ ) ;
93
+ } ;
94
+
95
+ test ( 'should use default environment for transaction and profile' , ( ) => {
96
+ mock = initTestClient ( ) ;
97
+
98
+ const transaction : Transaction = Sentry . startTransaction ( {
99
+ name : 'test-name' ,
100
+ } ) ;
101
+ transaction . finish ( ) ;
102
+
103
+ jest . runAllTimers ( ) ;
104
+
105
+ const envelope : Envelope | undefined = mock . transportSendMock . mock . lastCall ?. [ 0 ] ;
106
+ expectTransactionWithEnvironment ( envelope , 'mocked' ) ;
107
+ expectProfileWithEnvironment ( envelope , 'mocked' ) ;
108
+ } ) ;
109
+
110
+ test ( 'should use native environment for transaction and profile if user value is nullish' , ( ) => {
111
+ mock = initTestClient ( { withProfiling : true , environment : '' } ) ;
112
+
113
+ const transaction : Transaction = Sentry . startTransaction ( {
114
+ name : 'test-name' ,
115
+ } ) ;
116
+ transaction . finish ( ) ;
117
+
118
+ jest . runAllTimers ( ) ;
119
+
120
+ const envelope : Envelope | undefined = mock . transportSendMock . mock . lastCall ?. [ 0 ] ;
121
+ expectTransactionWithEnvironment ( envelope , 'native' ) ;
122
+ expectProfileWithEnvironment ( envelope , 'native' ) ;
123
+ } ) ;
124
+
125
+ test ( 'should keep nullish for transaction and profile uses default' , ( ) => {
126
+ mockWrapper . NATIVE . fetchNativeDeviceContexts . mockResolvedValue ( < NativeDeviceContextsResponse > {
127
+ environment : undefined ,
128
+ } ) ;
129
+ mock = initTestClient ( { withProfiling : true , environment : undefined } ) ;
130
+
131
+ const transaction : Transaction = Sentry . startTransaction ( {
132
+ name : 'test-name' ,
133
+ } ) ;
134
+ transaction . finish ( ) ;
135
+
136
+ jest . runAllTimers ( ) ;
137
+
138
+ const envelope : Envelope | undefined = mock . transportSendMock . mock . lastCall ?. [ 0 ] ;
139
+ expectTransactionWithEnvironment ( envelope , undefined ) ;
140
+ expectProfileWithEnvironment ( envelope , 'mocked' ) ;
141
+ } ) ;
142
+
143
+ test ( 'should keep custom environment for transaction and profile' , ( ) => {
144
+ mock = initTestClient ( { withProfiling : true , environment : 'custom' } ) ;
145
+
146
+ const transaction : Transaction = Sentry . startTransaction ( {
147
+ name : 'test-name' ,
148
+ } ) ;
149
+ transaction . finish ( ) ;
150
+
151
+ jest . runAllTimers ( ) ;
152
+
153
+ const envelope : Envelope | undefined = mock . transportSendMock . mock . lastCall ?. [ 0 ] ;
154
+ expectTransactionWithEnvironment ( envelope , 'custom' ) ;
155
+ expectProfileWithEnvironment ( envelope , 'custom' ) ;
156
+ } ) ;
157
+ } ) ;
158
+
68
159
describe ( 'with profiling enabled' , ( ) => {
69
160
beforeEach ( ( ) => {
70
161
mock = initTestClient ( ) ;
@@ -231,17 +322,24 @@ function getCurrentHermesProfilingIntegration(): TestHermesIntegration {
231
322
return integration as unknown as TestHermesIntegration ;
232
323
}
233
324
234
- function initTestClient ( withProfiling : boolean = true ) : {
325
+ function initTestClient (
326
+ testOptions : {
327
+ withProfiling ?: boolean ;
328
+ environment ?: string ;
329
+ } = {
330
+ withProfiling : true ,
331
+ } ,
332
+ ) : {
235
333
transportSendMock : jest . Mock < ReturnType < Transport [ 'send' ] > , Parameters < Transport [ 'send' ] > > ;
236
334
} {
237
335
const transportSendMock = jest . fn < ReturnType < Transport [ 'send' ] > , Parameters < Transport [ 'send' ] > > ( ) ;
238
- Sentry . init ( {
336
+ const options : Sentry . ReactNativeOptions = {
239
337
dsn : MOCK_DSN ,
240
338
_experiments : {
241
339
profilesSampleRate : 1 ,
242
340
} ,
243
341
integrations : integrations => {
244
- if ( ! withProfiling ) {
342
+ if ( ! testOptions . withProfiling ) {
245
343
return integrations . filter ( i => i . name !== 'HermesProfiling' ) ;
246
344
}
247
345
return integrations ;
@@ -250,7 +348,11 @@ function initTestClient(withProfiling: boolean = true): {
250
348
send : transportSendMock . mockResolvedValue ( undefined ) ,
251
349
flush : jest . fn ( ) . mockResolvedValue ( true ) ,
252
350
} ) ,
253
- } ) ;
351
+ } ;
352
+ if ( 'environment' in testOptions ) {
353
+ options . environment = testOptions . environment ;
354
+ }
355
+ Sentry . init ( options ) ;
254
356
255
357
// In production integrations are setup only once, but in the tests we want them to setup on every init
256
358
const integrations = Sentry . getCurrentHub ( ) . getClient ( ) ?. getOptions ( ) . integrations ;
0 commit comments