Skip to content

Commit 89373d6

Browse files
committed
use JSDOM
1 parent 4152918 commit 89373d6

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

packages/tracing-internal/test/browser/metrics/index.test.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { JSDOM } from 'jsdom';
12
import { Transaction } from '../../../src';
23
import type { ResourceEntry } from '../../../src/browser/metrics';
34
import { _addMeasureSpans, _addResourceSpans } from '../../../src/browser/metrics';
@@ -17,6 +18,13 @@ const mockWindowLocation = {
1718
} as Window['location'];
1819

1920
const originalWindowLocation = WINDOW.location;
21+
// @ts-expect-error store a reference so we can reset it later
22+
const globalDocument = global.document;
23+
// @ts-expect-error store a reference so we can reset it later
24+
const globalWindow = global.window;
25+
// @ts-expect-error store a reference so we can reset it later
26+
const globalLocation = global.location;
27+
2028
console.log('\n\n WINDOW!! \n\n');
2129
console.log(WINDOW.location);
2230
console.log('\n\n');
@@ -65,14 +73,11 @@ describe('_addResourceSpans', () => {
6573
const transaction = new Transaction({ op: 'pageload', name: '/' });
6674

6775
beforeAll(() => {
68-
WINDOW.location = mockWindowLocation;
69-
console.log('\n\n MOCK WINDOW!! \n\n');
70-
console.log(WINDOW.location);
71-
console.log('\n\n');
76+
setGlobalLocation(mockWindowLocation);
7277
});
7378

7479
afterAll(() => {
75-
WINDOW.location = originalWindowLocation;
80+
resetGlobalLocation();
7681
});
7782

7883
beforeEach(() => {
@@ -265,3 +270,26 @@ describe('_addResourceSpans', () => {
265270
);
266271
});
267272
});
273+
274+
const setGlobalLocation = (location: Location) => {
275+
const dom = new JSDOM();
276+
// @ts-expect-error need to override global document
277+
global.document = dom.window.document;
278+
// @ts-expect-error need to override global document
279+
global.window = dom.window;
280+
// @ts-expect-error need to override global document
281+
global.location = dom.window.location;
282+
283+
WINDOW.location = location;
284+
}
285+
286+
const resetGlobalLocation = () => {
287+
// @ts-expect-error need to override global document
288+
global.document = globalDocument;
289+
// @ts-expect-error need to override global document
290+
global.window = globalWindow;
291+
// @ts-expect-error need to override global document
292+
global.location = globalLocation;
293+
294+
WINDOW.location = originalWindowLocation;
295+
}

0 commit comments

Comments
 (0)