Skip to content

Commit

Permalink
✅ try to make recorder tests less flacky
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Jan 19, 2021
1 parent daf1360 commit d67bb18
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/rum-recorder/src/boot/recorder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,17 @@ describe('startRecording', () => {
const requestSendSpy = spyOn(HttpRequest.prototype, 'send')

waitRequests = (callback) => {
if (requestSendSpy.calls.first()) {
waitForLastRequest()
} else {
requestSendSpy.and.callFake(waitForLastRequest)
}

let isWaiting = false
function waitForLastRequest() {

requestSendSpy.and.callFake(() => {
if (isWaiting) {
return
}
isWaiting = true
setTimeout(() => {
callback(requestSendSpy.calls.allArgs().map(([data, size]) => ({ size, data: data as FormData })))
}, 300)
}
})
}
})

Expand Down Expand Up @@ -83,15 +78,19 @@ describe('startRecording', () => {

it('flushes the segment when its compressed data is getting too large', (done) => {
setupBuilder.build()
const clickCount = 10_000
const click = createNewEvent('click')
for (let i = 0; i < clickCount; i += 1) {
document.body.dispatchEvent(click)
const inputCount = 150
const textField = document.createElement('input')
const inputEvent = createNewEvent('input', { target: textField })
for (let i = 0; i < inputCount; i += 1) {
// Create a random value harder to deflate, so we don't have to send too many events to reach
// the limit.
textField.value = createRandomString(1000)
document.body.dispatchEvent(inputEvent)
}

waitRequests((requests) => {
expect(requests.length).toBe(1)
expect(requests[0].data.get('records_count')).toBe(String(clickCount + 2))
expect(requests[0].data.get('records_count')).toBe(String(inputCount + 2))
done()
})
})
Expand Down Expand Up @@ -146,3 +145,11 @@ function formDataAsObject(data: FormData) {
})
return result
}

function createRandomString(minLength: number) {
let result = ''
while (result.length < minLength) {
result += Math.random().toString(36)
}
return result
}

0 comments on commit d67bb18

Please sign in to comment.