@@ -3,7 +3,12 @@ import type * as Sentry from '@sentry/browser';
3
3
import type { EventEnvelopeHeaders } from '@sentry/types' ;
4
4
5
5
import { sentryTest } from '../../../utils/fixtures' ;
6
- import { envelopeRequestParser , shouldSkipTracingTest , waitForTransactionRequest } from '../../../utils/helpers' ;
6
+ import {
7
+ envelopeRequestParser ,
8
+ shouldSkipTracingTest ,
9
+ waitForErrorRequest ,
10
+ waitForTransactionRequest ,
11
+ } from '../../../utils/helpers' ;
7
12
import { getReplaySnapshot , shouldSkipReplayTest , waitForReplayRunning } from '../../../utils/replayHelpers' ;
8
13
9
14
type TestWindow = Window & {
@@ -216,3 +221,77 @@ sentryTest(
216
221
} ) ;
217
222
} ,
218
223
) ;
224
+
225
+ sentryTest ( 'should add replay_id to error DSC while replay is active' , async ( { getLocalTestPath, page } ) => {
226
+ if ( shouldSkipReplayTest ( ) ) {
227
+ sentryTest . skip ( ) ;
228
+ }
229
+
230
+ const hasTracing = ! shouldSkipTracingTest ( ) ;
231
+
232
+ await page . route ( 'https://dsn.ingest.sentry.io/**/*' , route => {
233
+ return route . fulfill ( {
234
+ status : 200 ,
235
+ contentType : 'application/json' ,
236
+ body : JSON . stringify ( { id : 'test-id' } ) ,
237
+ } ) ;
238
+ } ) ;
239
+
240
+ const url = await getLocalTestPath ( { testDir : __dirname } ) ;
241
+ await page . goto ( url ) ;
242
+
243
+ const error1Req = waitForErrorRequest ( page , event => event . exception ?. values ?. [ 0 ] . value === 'This is error #1' ) ;
244
+ const error2Req = waitForErrorRequest ( page , event => event . exception ?. values ?. [ 0 ] . value === 'This is error #2' ) ;
245
+
246
+ // We want to wait for the transaction to be done, to ensure we have a consistent test
247
+ const transactionReq = hasTracing ? waitForTransactionRequest ( page ) : Promise . resolve ( ) ;
248
+
249
+ // Wait for this to be available
250
+ await page . waitForFunction ( '!!window.Replay' ) ;
251
+
252
+ // We have to start replay before we finish the transaction, otherwise the DSC will not be frozen with the Replay ID
253
+ await page . evaluate ( 'window.Replay.start();' ) ;
254
+ await waitForReplayRunning ( page ) ;
255
+ await transactionReq ;
256
+
257
+ await page . evaluate ( 'window._triggerError(1)' ) ;
258
+
259
+ const error1Header = envelopeRequestParser ( await error1Req , 0 ) as EventEnvelopeHeaders ;
260
+ const replay = await getReplaySnapshot ( page ) ;
261
+
262
+ expect ( replay . session ?. id ) . toBeDefined ( ) ;
263
+
264
+ expect ( error1Header . trace ) . toBeDefined ( ) ;
265
+ expect ( error1Header . trace ) . toEqual ( {
266
+ environment : 'production' ,
267
+ trace_id : expect . any ( String ) ,
268
+ public_key : 'public' ,
269
+ replay_id : replay . session ?. id ,
270
+ ...( hasTracing
271
+ ? {
272
+ sample_rate : '1' ,
273
+ sampled : 'true' ,
274
+ }
275
+ : { } ) ,
276
+ } ) ;
277
+
278
+ // Now end replay and trigger another error, it should not have a replay_id in DSC anymore
279
+ await page . evaluate ( 'window.Replay.stop();' ) ;
280
+ await page . waitForFunction ( '!window.Replay.getReplayId();' ) ;
281
+ await page . evaluate ( 'window._triggerError(2)' ) ;
282
+
283
+ const error2Header = envelopeRequestParser ( await error2Req , 0 ) as EventEnvelopeHeaders ;
284
+
285
+ expect ( error2Header . trace ) . toBeDefined ( ) ;
286
+ expect ( error2Header . trace ) . toEqual ( {
287
+ environment : 'production' ,
288
+ trace_id : expect . any ( String ) ,
289
+ public_key : 'public' ,
290
+ ...( hasTracing
291
+ ? {
292
+ sample_rate : '1' ,
293
+ sampled : 'true' ,
294
+ }
295
+ : { } ) ,
296
+ } ) ;
297
+ } ) ;
0 commit comments