Skip to content

Commit 56179ed

Browse files
author
Luca Forstner
committed
Fix tests by initializing JSDom in the right places
1 parent 227cd4d commit 56179ed

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

packages/nextjs/test/index.client.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as SentryReact from '@sentry/react';
44
import { Integrations as TracingIntegrations } from '@sentry/tracing';
55
import { Integration } from '@sentry/types';
66
import { getGlobalObject, logger, SentryError } from '@sentry/utils';
7+
import { JSDOM } from 'jsdom';
78

89
import { init, Integrations, nextRouterInstrumentation } from '../src/index.client';
910
import { NextjsOptions } from '../src/utils/nextjsOptions';
@@ -16,6 +17,18 @@ const reactInit = jest.spyOn(SentryReact, 'init');
1617
const captureEvent = jest.spyOn(BaseClient.prototype, 'captureEvent');
1718
const logWarn = jest.spyOn(logger, 'warn');
1819

20+
// Set up JSDom - needed for page load instrumentation
21+
const dom = new JSDOM(undefined, { url: 'https://example.com/' });
22+
Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
23+
24+
const originalGlobalDocument = getGlobalObject<Window>().document;
25+
afterAll(() => {
26+
// Clean up JSDom
27+
Object.defineProperty(global, 'document', {
28+
value: originalGlobalDocument,
29+
});
30+
});
31+
1932
describe('Client init()', () => {
2033
afterEach(() => {
2134
jest.clearAllMocks();

packages/nextjs/test/performance/client.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,12 @@ describe('client', () => {
6060
{ url: pageProperties.url },
6161
);
6262

63-
Object.defineProperty(global, 'document', { value: dom.window.document, writable: true, configurable: true });
63+
Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
6464
}
6565

6666
afterEach(() => {
6767
// Clean up JSDom
68-
Object.defineProperty(global, 'document', {
69-
value: originalGlobalDocument,
70-
writable: true,
71-
configurable: true,
72-
});
68+
Object.defineProperty(global, 'document', { value: originalGlobalDocument });
7369
});
7470

7571
it('waits for Router.ready()', () => {

0 commit comments

Comments
 (0)