From fe33e68face4c75d0846537f57399e59a963ceb9 Mon Sep 17 00:00:00 2001 From: Thomas Lebeau Date: Mon, 29 Jul 2024 19:49:57 +0200 Subject: [PATCH] do not override onunhandledrejection if it does not exsits --- .../src/domain/error/trackRuntimeError.spec.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/core/src/domain/error/trackRuntimeError.spec.ts b/packages/core/src/domain/error/trackRuntimeError.spec.ts index 62e5a6bde1..e3437cafc5 100644 --- a/packages/core/src/domain/error/trackRuntimeError.spec.ts +++ b/packages/core/src/domain/error/trackRuntimeError.spec.ts @@ -10,7 +10,6 @@ describe('trackRuntimeError', () => { let originalOnErrorHandler: OnErrorEventHandler let onErrorSpy: jasmine.Spy - let onUnhandledrejectionSpy: jasmine.Spy | null let notifyError: jasmine.Spy let stopRuntimeErrorTracking: () => void @@ -18,7 +17,6 @@ describe('trackRuntimeError', () => { originalOnErrorHandler = window.onerror onErrorSpy = jasmine.createSpy() window.onerror = onErrorSpy - onUnhandledrejectionSpy = setupOnUnhandledrejectionSpy() notifyError = jasmine.createSpy() const errorObservable = new Observable() errorObservable.subscribe((e: RawError) => notifyError(e) as void) @@ -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() }) @@ -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 }