From a90a585c7f458bd0e81411831b0dd44d4f31922f Mon Sep 17 00:00:00 2001 From: Aymeric Mortemousque Date: Mon, 18 Nov 2024 14:45:22 +0100 Subject: [PATCH 1/2] Fix test cleanup tasks order --- .../core/src/tools/instrumentMethod.spec.ts | 3 +-- packages/core/src/tools/timer.spec.ts | 2 +- .../test/emulate/mockReportingObserver.ts | 12 +++++++-- packages/core/test/leakDetection.ts | 25 ++++++++++--------- packages/core/test/registerCleanupTask.ts | 2 +- .../src/domain/error/trackReportError.spec.ts | 12 ++++----- 6 files changed, 32 insertions(+), 24 deletions(-) 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/emulate/mockReportingObserver.ts b/packages/core/test/emulate/mockReportingObserver.ts index 1f034f4e62..a278ece395 100644 --- a/packages/core/test/emulate/mockReportingObserver.ts +++ b/packages/core/test/emulate/mockReportingObserver.ts @@ -50,8 +50,16 @@ export function mockReportingObserver() { export type MockCspEventListener = ReturnType export function mockCspEventListener() { - spyOn(document, 'addEventListener').and.callFake((_type: string, listener: EventListener) => { - listeners.push(listener) + // eslint-disable-next-line @typescript-eslint/unbound-method + const originalAddEventListener = EventTarget.prototype.addEventListener + EventTarget.prototype.addEventListener = jasmine + .createSpy() + .and.callFake((_type: string, listener: EventListener) => { + listeners.push(listener) + }) + + registerCleanupTask(() => { + EventTarget.prototype.addEventListener = originalAddEventListener }) const listeners: EventListener[] = [] 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', () => { From 4480edcbd01e068374d1fb48ac1e735396f2ad79 Mon Sep 17 00:00:00 2001 From: Aymeric Mortemousque Date: Mon, 18 Nov 2024 16:36:36 +0100 Subject: [PATCH 2/2] Fix wrong update --- packages/core/test/emulate/mockReportingObserver.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/core/test/emulate/mockReportingObserver.ts b/packages/core/test/emulate/mockReportingObserver.ts index a278ece395..1f034f4e62 100644 --- a/packages/core/test/emulate/mockReportingObserver.ts +++ b/packages/core/test/emulate/mockReportingObserver.ts @@ -50,16 +50,8 @@ export function mockReportingObserver() { export type MockCspEventListener = ReturnType export function mockCspEventListener() { - // eslint-disable-next-line @typescript-eslint/unbound-method - const originalAddEventListener = EventTarget.prototype.addEventListener - EventTarget.prototype.addEventListener = jasmine - .createSpy() - .and.callFake((_type: string, listener: EventListener) => { - listeners.push(listener) - }) - - registerCleanupTask(() => { - EventTarget.prototype.addEventListener = originalAddEventListener + spyOn(document, 'addEventListener').and.callFake((_type: string, listener: EventListener) => { + listeners.push(listener) }) const listeners: EventListener[] = []