Skip to content

Commit 92253a5

Browse files
✅ [RUM-10144] prepare tests for async assembly
This commit is purely intending to ease review. It does not introduce any functional change or new test case, it just prepares the tests for the assembly being asynchronous coming in the next commit.
1 parent 6b29b3e commit 92253a5

2 files changed

Lines changed: 168 additions & 117 deletions

File tree

packages/rum-core/src/boot/startRum.spec.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function collectServerEvents(lifeCycle: LifeCycle) {
5858
return serverRumEvents
5959
}
6060

61-
function startRumStub(
61+
async function startRumStub(
6262
lifeCycle: LifeCycle,
6363
configuration: RumConfiguration,
6464
sessionManager: RumSessionManager,
@@ -96,6 +96,10 @@ function startRumStub(
9696
)
9797

9898
startLongAnimationFrameCollection(lifeCycle, configuration)
99+
100+
// Wait for assembly to start producing events
101+
await Promise.resolve()
102+
99103
return {
100104
stop: () => {
101105
viewHistory.stop()
@@ -111,15 +115,15 @@ describe('rum session', () => {
111115
let lifeCycle: LifeCycle
112116
let sessionManager: RumSessionManagerMock
113117

114-
beforeEach(() => {
118+
beforeEach(async () => {
115119
lifeCycle = new LifeCycle()
116120
sessionManager = createRumSessionManagerMock().setId('42')
117121
const domMutationObservable = new Observable<RumMutationRecord[]>()
118122
const windowOpenObservable = new Observable<void>()
119123
const { locationChangeObservable } = setupLocationObserver()
120124

121125
serverRumEvents = collectServerEvents(lifeCycle)
122-
const { stop } = startRumStub(
126+
const { stop } = await startRumStub(
123127
lifeCycle,
124128
mockRumConfiguration(),
125129
sessionManager,
@@ -160,7 +164,7 @@ describe('rum session keep alive', () => {
160164
let sessionManager: RumSessionManagerMock
161165
let serverRumEvents: RumEvent[]
162166

163-
beforeEach(() => {
167+
beforeEach(async () => {
164168
lifeCycle = new LifeCycle()
165169
clock = mockClock()
166170
sessionManager = createRumSessionManagerMock().setId('1234')
@@ -169,7 +173,7 @@ describe('rum session keep alive', () => {
169173
const { locationChangeObservable } = setupLocationObserver()
170174

171175
serverRumEvents = collectServerEvents(lifeCycle)
172-
const { stop } = startRumStub(
176+
const { stop } = await startRumStub(
173177
lifeCycle,
174178
mockRumConfiguration(),
175179
sessionManager,
@@ -227,14 +231,14 @@ describe('rum events url', () => {
227231
let serverRumEvents: RumEvent[]
228232
let stop: () => void
229233

230-
function setupViewUrlTest() {
234+
async function setupViewUrlTest() {
231235
const sessionManager = createRumSessionManagerMock().setId('1234')
232236
const domMutationObservable = new Observable<RumMutationRecord[]>()
233237
const windowOpenObservable = new Observable<void>()
234238
const locationSetupResult = setupLocationObserver('http://foo.com/')
235239
changeLocation = locationSetupResult.changeLocation
236240

237-
const startResult = startRumStub(
241+
const startResult = await startRumStub(
238242
lifeCycle,
239243
mockRumConfiguration(),
240244
sessionManager,
@@ -258,8 +262,8 @@ describe('rum events url', () => {
258262
})
259263
})
260264

261-
it('should keep the same URL when updating a view ended by a URL change', () => {
262-
setupViewUrlTest()
265+
it('should keep the same URL when updating a view ended by a URL change', async () => {
266+
await setupViewUrlTest()
263267
serverRumEvents.length = 0
264268

265269
changeLocation('/bar')
@@ -269,11 +273,11 @@ describe('rum events url', () => {
269273
expect(serverRumEvents[1].view.url).toEqual('http://foo.com/bar')
270274
})
271275

272-
it('should attach the url corresponding to the start of the event', () => {
276+
it('should attach the url corresponding to the start of the event', async () => {
273277
clock = mockClock()
274278
const { notifyPerformanceEntries } = mockPerformanceObserver()
275279

276-
setupViewUrlTest()
280+
await setupViewUrlTest()
277281
clock.tick(10)
278282
changeLocation('http://foo.com/?bar=bar')
279283
clock.tick(10)
@@ -296,10 +300,10 @@ describe('rum events url', () => {
296300
expect(longTaskEvent.view.url).toBe('http://foo.com/?bar=bar')
297301
})
298302

299-
it('should keep the same URL when updating an ended view', () => {
303+
it('should keep the same URL when updating an ended view', async () => {
300304
clock = mockClock()
301305
const { triggerOnLoad } = mockDocumentReadyState()
302-
setupViewUrlTest()
306+
await setupViewUrlTest()
303307

304308
clock.tick(VIEW_DURATION)
305309

@@ -320,7 +324,7 @@ describe('view events', () => {
320324
let interceptor: ReturnType<typeof interceptRequests>
321325
let stop: () => void
322326

323-
function setupViewCollectionTest() {
327+
async function setupViewCollectionTest() {
324328
const startResult = startRum(
325329
mockRumConfiguration(),
326330
noopRecorderApi,
@@ -331,6 +335,9 @@ describe('view events', () => {
331335
createCustomVitalsState()
332336
)
333337

338+
// Wait for assembly to start producing events
339+
await Promise.resolve()
340+
334341
stop = startResult.stop
335342
interceptor = interceptRequests()
336343
}
@@ -344,14 +351,14 @@ describe('view events', () => {
344351
})
345352
})
346353

347-
it('sends a view update on page unload when bridge is absent', () => {
354+
it('sends a view update on page unload when bridge is absent', async () => {
348355
// Note: this test is intentionally very high level to make sure the view update is correctly
349356
// made right before flushing the Batch.
350357

351358
// Arbitrary duration to simulate a non-zero view duration
352359
const VIEW_DURATION = ONE_SECOND as Duration
353360

354-
setupViewCollectionTest()
361+
await setupViewCollectionTest()
355362

356363
clock.tick(VIEW_DURATION - relativeNow())
357364
window.dispatchEvent(createNewEvent('beforeunload'))
@@ -367,13 +374,13 @@ describe('view events', () => {
367374
expect(lastRumViewEvent.view.time_spent).toBe(toServerDuration(VIEW_DURATION))
368375
})
369376

370-
it('sends a view update on page unload when bridge is present', () => {
377+
it('sends a view update on page unload when bridge is present', async () => {
371378
const eventBridge = mockEventBridge()
372379
const sendSpy = spyOn(eventBridge, 'send')
373380

374381
const VIEW_DURATION = ONE_SECOND as Duration
375382

376-
setupViewCollectionTest()
383+
await setupViewCollectionTest()
377384

378385
clock.tick(VIEW_DURATION - relativeNow())
379386
window.dispatchEvent(createNewEvent('beforeunload'))

0 commit comments

Comments
 (0)