Skip to content

Commit

Permalink
do not override onunhandledrejection if it does not exsits
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-lebeau committed Jul 29, 2024
1 parent 161f1d3 commit fe33e68
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/core/src/domain/error/trackRuntimeError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ describe('trackRuntimeError', () => {

let originalOnErrorHandler: OnErrorEventHandler
let onErrorSpy: jasmine.Spy
let onUnhandledrejectionSpy: jasmine.Spy | null
let notifyError: jasmine.Spy
let stopRuntimeErrorTracking: () => void

beforeEach(() => {
originalOnErrorHandler = window.onerror
onErrorSpy = jasmine.createSpy()
window.onerror = onErrorSpy
onUnhandledrejectionSpy = setupOnUnhandledrejectionSpy()
notifyError = jasmine.createSpy()
const errorObservable = new Observable<RawError>()
errorObservable.subscribe((e: RawError) => notifyError(e) as void)
Expand Down Expand Up @@ -49,13 +47,15 @@ describe('trackRuntimeError', () => {
pending('onunhandledrejection not supported')
}

const onUnhandledrejectionSpy = setupOnUnhandledrejectionSpy()

disableJasmineUncaughtExceptionTracking()

setTimeout(() => {
void Promise.reject(new Error(ERROR_MESSAGE))
})

collectAsyncCalls(onUnhandledrejectionSpy as jasmine.Spy, 1, () => {
collectAsyncCalls(onUnhandledrejectionSpy, 1, () => {
expect(notifyError).toHaveBeenCalledOnceWith(jasmine.objectContaining({ message: ERROR_MESSAGE }))
done()
})
Expand Down Expand Up @@ -314,14 +314,16 @@ describe('instrumentUnhandledRejection', () => {
})

function setupOnUnhandledrejectionSpy() {
const originalOnUnhandledRejectionHandler: Window['onunhandledrejection'] = window.onunhandledrejection
const onUnhandledrejectionSpy = jasmine.createSpy()
const originalOnUnhandledRejectionHandler: Window['onunhandledrejection'] = window.onunhandledrejection

window.onunhandledrejection = onUnhandledrejectionSpy
if ('onunhandledrejection' in window) {
window.onunhandledrejection = onUnhandledrejectionSpy

registerCleanupTask(() => {
window.onunhandledrejection = originalOnUnhandledRejectionHandler
})
registerCleanupTask(() => {
window.onunhandledrejection = originalOnUnhandledRejectionHandler
})
}

return onUnhandledrejectionSpy
}

0 comments on commit fe33e68

Please sign in to comment.