@@ -7,8 +7,9 @@ import type { Envelope, Event, Profile, Transaction, Transport } from '@sentry/t
77
88import * as Sentry from '../../src/js' ;
99import { HermesProfiling } from '../../src/js/integrations' ;
10+ import type { NativeDeviceContextsResponse } from '../../src/js/NativeRNSentry' ;
1011import 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' ;
1213import { RN_GLOBAL_OBJ } from '../../src/js/utils/worldwide' ;
1314import { MOCK_DSN } from '../mockDsn' ;
1415import { envelopeItemPayload , envelopeItems } from '../testutils' ;
@@ -35,7 +36,7 @@ describe('profiling integration', () => {
3536 } ) ;
3637
3738 test ( 'should start profile if there is a transaction running when integration is created' , ( ) => {
38- mock = initTestClient ( false ) ;
39+ mock = initTestClient ( { withProfiling : false } ) ;
3940 jest . runAllTimers ( ) ;
4041 jest . clearAllMocks ( ) ;
4142
@@ -65,6 +66,96 @@ describe('profiling integration', () => {
6566 ] ) ;
6667 } ) ;
6768
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+
68159 describe ( 'with profiling enabled' , ( ) => {
69160 beforeEach ( ( ) => {
70161 mock = initTestClient ( ) ;
@@ -231,17 +322,24 @@ function getCurrentHermesProfilingIntegration(): TestHermesIntegration {
231322 return integration as unknown as TestHermesIntegration ;
232323}
233324
234- function initTestClient ( withProfiling : boolean = true ) : {
325+ function initTestClient (
326+ testOptions : {
327+ withProfiling ?: boolean ;
328+ environment ?: string ;
329+ } = {
330+ withProfiling : true ,
331+ } ,
332+ ) : {
235333 transportSendMock : jest . Mock < ReturnType < Transport [ 'send' ] > , Parameters < Transport [ 'send' ] > > ;
236334} {
237335 const transportSendMock = jest . fn < ReturnType < Transport [ 'send' ] > , Parameters < Transport [ 'send' ] > > ( ) ;
238- Sentry . init ( {
336+ const options : Sentry . ReactNativeOptions = {
239337 dsn : MOCK_DSN ,
240338 _experiments : {
241339 profilesSampleRate : 1 ,
242340 } ,
243341 integrations : integrations => {
244- if ( ! withProfiling ) {
342+ if ( ! testOptions . withProfiling ) {
245343 return integrations . filter ( i => i . name !== 'HermesProfiling' ) ;
246344 }
247345 return integrations ;
@@ -250,7 +348,11 @@ function initTestClient(withProfiling: boolean = true): {
250348 send : transportSendMock . mockResolvedValue ( undefined ) ,
251349 flush : jest . fn ( ) . mockResolvedValue ( true ) ,
252350 } ) ,
253- } ) ;
351+ } ;
352+ if ( 'environment' in testOptions ) {
353+ options . environment = testOptions . environment ;
354+ }
355+ Sentry . init ( options ) ;
254356
255357 // In production integrations are setup only once, but in the tests we want them to setup on every init
256358 const integrations = Sentry . getCurrentHub ( ) . getClient ( ) ?. getOptions ( ) . integrations ;
0 commit comments