3
3
*/
4
4
5
5
import type { MockInstance , MockedFunction } from 'vitest' ;
6
- import { afterAll , afterEach , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
6
+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
7
7
8
8
import * as SentryBrowserUtils from '@sentry-internal/browser-utils' ;
9
9
10
10
import { WINDOW } from '../../src/constants' ;
11
11
import type { Replay } from '../../src/integration' ;
12
12
import type { ReplayContainer } from '../../src/replay' ;
13
- import { clearSession } from '../../src/session/clearSession' ;
14
13
import { addEvent } from '../../src/util/addEvent' ;
15
14
import { createOptionsEvent } from '../../src/util/handleRecordingEmit' ;
16
15
// mock functions need to be imported first
@@ -32,7 +31,7 @@ describe('Integration | stop', () => {
32
31
let mockAddDomInstrumentationHandler : MockInstance ;
33
32
let mockRunFlush : MockRunFlush ;
34
33
35
- beforeAll ( async ( ) => {
34
+ beforeEach ( async ( ) => {
36
35
vi . setSystemTime ( new Date ( BASE_TIMESTAMP ) ) ;
37
36
mockAddDomInstrumentationHandler = vi . spyOn ( SentryBrowserUtils , 'addClickKeypressInstrumentationHandler' ) ;
38
37
@@ -41,33 +40,18 @@ describe('Integration | stop', () => {
41
40
// @ts -expect-error private API
42
41
mockRunFlush = vi . spyOn ( replay , '_runFlush' ) ;
43
42
44
- vi . runAllTimers ( ) ;
45
- } ) ;
46
-
47
- beforeEach ( ( ) => {
48
- vi . setSystemTime ( new Date ( BASE_TIMESTAMP ) ) ;
49
- replay . eventBuffer ?. destroy ( ) ;
43
+ await vi . runAllTimersAsync ( ) ;
50
44
vi . clearAllMocks ( ) ;
51
45
} ) ;
52
46
53
47
afterEach ( async ( ) => {
54
- vi . runAllTimers ( ) ;
55
- await new Promise ( process . nextTick ) ;
56
48
vi . setSystemTime ( new Date ( BASE_TIMESTAMP ) ) ;
57
- sessionStorage . clear ( ) ;
58
- clearSession ( replay ) ;
59
- replay [ '_initializeSessionForSampling' ] ( ) ;
60
- replay . setInitialState ( ) ;
61
- mockRecord . takeFullSnapshot . mockClear ( ) ;
62
- mockAddDomInstrumentationHandler . mockClear ( ) ;
49
+ integration && ( await integration . stop ( ) ) ;
63
50
Object . defineProperty ( WINDOW , 'location' , {
64
51
value : prevLocation ,
65
52
writable : true ,
66
53
} ) ;
67
- } ) ;
68
-
69
- afterAll ( ( ) => {
70
- integration && integration . stop ( ) ;
54
+ vi . clearAllMocks ( ) ;
71
55
} ) ;
72
56
73
57
it ( 'does not upload replay if it was stopped and can resume replays afterwards' , async ( ) => {
@@ -106,7 +90,7 @@ describe('Integration | stop', () => {
106
90
107
91
vi . advanceTimersByTime ( ELAPSED ) ;
108
92
109
- const timestamp = + new Date ( BASE_TIMESTAMP + ELAPSED + ELAPSED ) / 1000 ;
93
+ const timestamp = + new Date ( BASE_TIMESTAMP + ELAPSED + ELAPSED + ELAPSED ) / 1000 ;
110
94
111
95
const hiddenBreadcrumb = {
112
96
type : 5 ,
@@ -123,15 +107,15 @@ describe('Integration | stop', () => {
123
107
124
108
addEvent ( replay , TEST_EVENT ) ;
125
109
WINDOW . dispatchEvent ( new Event ( 'blur' ) ) ;
126
- vi . runAllTimers ( ) ;
110
+ await vi . runAllTimersAsync ( ) ;
127
111
await new Promise ( process . nextTick ) ;
128
112
expect ( replay ) . toHaveLastSentReplay ( {
129
113
recordingPayloadHeader : { segment_id : 0 } ,
130
114
recordingData : JSON . stringify ( [
131
115
// This event happens when we call `replay.start`
132
116
{
133
117
data : { isCheckout : true } ,
134
- timestamp : BASE_TIMESTAMP + ELAPSED ,
118
+ timestamp : BASE_TIMESTAMP + ELAPSED + ELAPSED ,
135
119
type : 2 ,
136
120
} ,
137
121
optionsEvent ,
@@ -142,12 +126,14 @@ describe('Integration | stop', () => {
142
126
143
127
// Session's last activity is last updated when we call `setup()` and *NOT*
144
128
// when tab is blurred
145
- expect ( replay . session ?. lastActivity ) . toBe ( BASE_TIMESTAMP + ELAPSED ) ;
129
+ expect ( replay . session ?. lastActivity ) . toBe ( BASE_TIMESTAMP + ELAPSED + ELAPSED ) ;
146
130
} ) ;
147
131
148
132
it ( 'does not buffer new events after being stopped' , async function ( ) {
133
+ expect ( replay . eventBuffer ?. hasEvents ) . toBe ( false ) ;
134
+ expect ( mockRunFlush ) . toHaveBeenCalledTimes ( 0 ) ;
149
135
const TEST_EVENT = getTestEventIncremental ( { timestamp : BASE_TIMESTAMP } ) ;
150
- addEvent ( replay , TEST_EVENT ) ;
136
+ addEvent ( replay , TEST_EVENT , true ) ;
151
137
expect ( replay . eventBuffer ?. hasEvents ) . toBe ( true ) ;
152
138
expect ( mockRunFlush ) . toHaveBeenCalledTimes ( 0 ) ;
153
139
0 commit comments