diff --git a/packages/core/src/tools/instrumentMethod.spec.ts b/packages/core/src/tools/instrumentMethod.spec.ts index c2f2988a55..dffe604a1d 100644 --- a/packages/core/src/tools/instrumentMethod.spec.ts +++ b/packages/core/src/tools/instrumentMethod.spec.ts @@ -204,11 +204,10 @@ describe('instrumentSetter', () => { beforeEach(() => { clock = mockClock() - zoneJs = mockZoneJs() - registerCleanupTask(() => { clock.cleanup() }) + zoneJs = mockZoneJs() }) it('replaces the original setter', () => { diff --git a/packages/core/src/tools/timer.spec.ts b/packages/core/src/tools/timer.spec.ts index f1c4bff7df..4bbc21fa57 100644 --- a/packages/core/src/tools/timer.spec.ts +++ b/packages/core/src/tools/timer.spec.ts @@ -21,11 +21,11 @@ import { noop } from './utils/functionUtils' beforeEach(() => { clock = mockClock() - zoneJs = mockZoneJs() registerCleanupTask(() => { clock.cleanup() resetMonitor() }) + zoneJs = mockZoneJs() }) it('executes the callback asynchronously', () => { diff --git a/packages/core/test/leakDetection.ts b/packages/core/test/leakDetection.ts index 523e73a5a0..01d7f001a8 100644 --- a/packages/core/test/leakDetection.ts +++ b/packages/core/test/leakDetection.ts @@ -1,23 +1,21 @@ import { display } from '../src/tools/display' import { isIE } from '../src/tools/utils/browserDetection' import { getCurrentJasmineSpec } from './getCurrentJasmineSpec' - -let originalAddEventListener: typeof EventTarget.prototype.addEventListener -let originalRemoveEventListener: typeof EventTarget.prototype.removeEventListener -let wrappedListeners: { - [key: string]: Map -} +import { registerCleanupTask } from './registerCleanupTask' export function startLeakDetection() { if (isIE()) { return } - wrappedListeners = {} + + let wrappedListeners: { + [key: string]: Map + } = {} // eslint-disable-next-line @typescript-eslint/unbound-method - originalAddEventListener = EventTarget.prototype.addEventListener + const originalAddEventListener = EventTarget.prototype.addEventListener // eslint-disable-next-line @typescript-eslint/unbound-method - originalRemoveEventListener = EventTarget.prototype.removeEventListener + const originalRemoveEventListener = EventTarget.prototype.removeEventListener EventTarget.prototype.addEventListener = function (event, listener, options) { if (!wrappedListeners[event]) { @@ -32,15 +30,18 @@ export function startLeakDetection() { wrappedListeners[event]?.delete(listener) return originalRemoveEventListener.call(this, event, wrappedListener || listener, options) } + + registerCleanupTask(() => { + EventTarget.prototype.addEventListener = originalAddEventListener + EventTarget.prototype.removeEventListener = originalRemoveEventListener + wrappedListeners = {} + }) } export function stopLeakDetection() { if (isIE()) { return } - EventTarget.prototype.addEventListener = originalAddEventListener - EventTarget.prototype.removeEventListener = originalRemoveEventListener - wrappedListeners = {} } function withLeakDetection(eventName: string, listener: EventListener) { diff --git a/packages/core/test/registerCleanupTask.ts b/packages/core/test/registerCleanupTask.ts index 29c00403db..fcc3ad459a 100644 --- a/packages/core/test/registerCleanupTask.ts +++ b/packages/core/test/registerCleanupTask.ts @@ -1,7 +1,7 @@ const cleanupTasks: Array<() => void> = [] export function registerCleanupTask(task: () => void) { - cleanupTasks.push(task) + cleanupTasks.unshift(task) } afterEach(() => { diff --git a/packages/rum-core/src/domain/error/trackReportError.spec.ts b/packages/rum-core/src/domain/error/trackReportError.spec.ts index c5a477d3b3..095897b501 100644 --- a/packages/rum-core/src/domain/error/trackReportError.spec.ts +++ b/packages/rum-core/src/domain/error/trackReportError.spec.ts @@ -7,6 +7,7 @@ import { mockClock, mockCspEventListener, mockReportingObserver, + registerCleanupTask, } from '@datadog/browser-core/test' import { mockRumConfiguration } from '../../../test' import type { RumConfiguration } from '../configuration' @@ -27,13 +28,12 @@ describe('trackReportError', () => { notifyLog = jasmine.createSpy('notifyLog') reportingObserver = mockReportingObserver() subscription = errorObservable.subscribe(notifyLog) - cspEventListener = mockCspEventListener() clock = mockClock() - }) - - afterEach(() => { - subscription.unsubscribe() - clock.cleanup() + registerCleanupTask(() => { + subscription.unsubscribe() + clock.cleanup() + }) + cspEventListener = mockCspEventListener() }) it('should track reports', () => {