Skip to content

Commit dadb1bb

Browse files
Lms24cursoragent
andcommitted
test(nextjs): Stub removeEventListener in fake-window test harnesses
The deferred browser session capture schedules a `whenIdleOrHidden` timer that calls `WINDOW.removeEventListener` when it fires. These nextjs tests stub a fake window with `addEventListener` but not `removeEventListener`, so the deferred timer threw an unhandled `TypeError` after the test. Stub and restore `removeEventListener` alongside `addEventListener`. Co-Authored-By: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 5d52db4 commit dadb1bb

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

packages/nextjs/test/clientSdk.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@ const dom = new JSDOM(undefined, { url: 'https://example.com/' });
1717
Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
1818
Object.defineProperty(global, 'location', { value: dom.window.document.location, writable: true });
1919
Object.defineProperty(global, 'addEventListener', { value: () => undefined, writable: true });
20+
Object.defineProperty(global, 'removeEventListener', { value: () => undefined, writable: true });
2021

2122
const originalGlobalDocument = WINDOW.document;
2223
const originalGlobalLocation = WINDOW.location;
2324
const originalNavigator = WINDOW.navigator;
2425
// eslint-disable-next-line @typescript-eslint/unbound-method
2526
const originalGlobalAddEventListener = WINDOW.addEventListener;
27+
// eslint-disable-next-line @typescript-eslint/unbound-method
28+
const originalGlobalRemoveEventListener = WINDOW.removeEventListener;
2629

2730
afterAll(() => {
2831
// Clean up JSDom
2932
Object.defineProperty(WINDOW, 'document', { value: originalGlobalDocument });
3033
Object.defineProperty(WINDOW, 'location', { value: originalGlobalLocation });
3134
Object.defineProperty(WINDOW, 'navigator', { value: originalNavigator, writable: true, configurable: true });
3235
Object.defineProperty(WINDOW, 'addEventListener', { value: originalGlobalAddEventListener });
36+
Object.defineProperty(WINDOW, 'removeEventListener', { value: originalGlobalRemoveEventListener });
3337
});
3438

3539
function findIntegrationByName(integrations: Integration[] = [], name: string): Integration | undefined {

packages/nextjs/test/config/conflictingDebugOptions.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('debug: true + removeDebugLogging warning', () => {
1919
let originalDocument: unknown;
2020
let originalLocation: unknown;
2121
let originalAddEventListener: unknown;
22+
let originalRemoveEventListener: unknown;
2223

2324
beforeAll(async () => {
2425
// Pre-warm V8 compilation cache for the large SDK module graphs.
@@ -34,16 +35,19 @@ describe('debug: true + removeDebugLogging warning', () => {
3435
originalDocument = (globalThis as any).document;
3536
originalLocation = (globalThis as any).location;
3637
originalAddEventListener = (globalThis as any).addEventListener;
38+
originalRemoveEventListener = (globalThis as any).removeEventListener;
3739

3840
Object.defineProperty(globalThis, 'document', { value: dom.window.document, writable: true });
3941
Object.defineProperty(globalThis, 'location', { value: dom.window.location, writable: true });
4042
Object.defineProperty(globalThis, 'addEventListener', { value: () => undefined, writable: true });
43+
Object.defineProperty(globalThis, 'removeEventListener', { value: () => undefined, writable: true });
4144
});
4245

4346
afterAll(() => {
4447
Object.defineProperty(globalThis, 'document', { value: originalDocument, writable: true });
4548
Object.defineProperty(globalThis, 'location', { value: originalLocation, writable: true });
4649
Object.defineProperty(globalThis, 'addEventListener', { value: originalAddEventListener, writable: true });
50+
Object.defineProperty(globalThis, 'removeEventListener', { value: originalRemoveEventListener, writable: true });
4751
});
4852

4953
afterEach(() => {

0 commit comments

Comments
 (0)