1
+ import { JSDOM } from 'jsdom' ;
1
2
import { Transaction } from '../../../src' ;
2
3
import type { ResourceEntry } from '../../../src/browser/metrics' ;
3
4
import { _addMeasureSpans , _addResourceSpans } from '../../../src/browser/metrics' ;
@@ -17,6 +18,13 @@ const mockWindowLocation = {
17
18
} as Window [ 'location' ] ;
18
19
19
20
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
+
20
28
console . log ( '\n\n WINDOW!! \n\n' ) ;
21
29
console . log ( WINDOW . location ) ;
22
30
console . log ( '\n\n' ) ;
@@ -65,14 +73,11 @@ describe('_addResourceSpans', () => {
65
73
const transaction = new Transaction ( { op : 'pageload' , name : '/' } ) ;
66
74
67
75
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 ) ;
72
77
} ) ;
73
78
74
79
afterAll ( ( ) => {
75
- WINDOW . location = originalWindowLocation ;
80
+ resetGlobalLocation ( ) ;
76
81
} ) ;
77
82
78
83
beforeEach ( ( ) => {
@@ -265,3 +270,26 @@ describe('_addResourceSpans', () => {
265
270
) ;
266
271
} ) ;
267
272
} ) ;
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