@@ -13,6 +13,8 @@ let performanceObserverCallback
1313let scriptTrackerModule
1414// Allows tests to control Error.stack content
1515let mockStack = null
16+ // Control what getNavigationEntry returns
17+ let mockNavigationEntry = null
1618
1719// Helper to construct error with custom stack
1820const OriginalError = global . Error
@@ -26,12 +28,18 @@ class MockError extends OriginalError {
2628 }
2729}
2830
31+ jest . mock ( '../../../../src/common/constants/runtime' , ( ) => ( {
32+ globalScope : global . window ,
33+ getNavigationEntry : ( ) => mockNavigationEntry
34+ } ) )
35+
2936beforeEach ( ( ) => {
3037 // Reset module state to ensure clean test isolation
3138 jest . resetModules ( )
3239 jest . clearAllMocks ( )
3340 performanceObserverCallback = null
3441 mockStack = null
42+ mockNavigationEntry = null
3543
3644 // Setup custom Error class to control stack traces
3745 global . Error = MockError
@@ -97,9 +105,14 @@ describe('script-tracker', () => {
97105
98106 test ( 'identifies inline script when URL matches navigation' , ( ) => {
99107 // Setup: Mock navigation entry with page URL
108+ mockNavigationEntry = { name : 'https://example.com/page.html' }
109+
100110 global . performance . getEntriesByType = jest . fn ( ( type ) => {
101111 if ( type === 'navigation' ) {
102- return [ { initiatorType : 'navigation' , name : 'https://example.com/page.html' } ]
112+ return [ mockNavigationEntry ]
113+ }
114+ if ( type === 'resource' ) {
115+ return [ ]
103116 }
104117 return [ ]
105118 } )
@@ -127,10 +140,9 @@ describe('script-tracker', () => {
127140 responseEnd : 250.8
128141 }
129142
143+ mockNavigationEntry = { name : 'https://example.com/' }
144+
130145 global . performance . getEntriesByType = jest . fn ( ( type ) => {
131- if ( type === 'navigation' ) {
132- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
133- }
134146 if ( type === 'resource' ) {
135147 return [ mockResourceEntry ]
136148 }
@@ -163,13 +175,9 @@ describe('script-tracker', () => {
163175 }
164176
165177 // Setup: Script not in static buffer, only in observer
178+ mockNavigationEntry = { name : 'https://example.com/' }
179+
166180 global . performance . getEntriesByType = jest . fn ( ( type ) => {
167- if ( type === 'navigation' ) {
168- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
169- }
170- if ( type === 'resource' ) {
171- return [ ] // Not in static buffer
172- }
173181 return [ ]
174182 } )
175183
@@ -209,10 +217,9 @@ describe('script-tracker', () => {
209217 responseEnd : 120.5
210218 }
211219
220+ mockNavigationEntry = { name : 'https://example.com/' }
221+
212222 global . performance . getEntriesByType = jest . fn ( ( type ) => {
213- if ( type === 'navigation' ) {
214- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
215- }
216223 return [ ]
217224 } )
218225
@@ -254,10 +261,9 @@ describe('script-tracker', () => {
254261 return [ ]
255262 } )
256263
264+ mockNavigationEntry = { name : 'https://example.com/' }
265+
257266 global . performance . getEntriesByType = jest . fn ( ( type ) => {
258- if ( type === 'navigation' ) {
259- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
260- }
261267 return [ ]
262268 } )
263269
@@ -294,10 +300,9 @@ describe('script-tracker', () => {
294300 return [ ]
295301 } )
296302
303+ mockNavigationEntry = { name : 'https://example.com/' }
304+
297305 global . performance . getEntriesByType = jest . fn ( ( type ) => {
298- if ( type === 'navigation' ) {
299- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
300- }
301306 return [ ]
302307 } )
303308
@@ -426,10 +431,9 @@ describe('script-tracker', () => {
426431 responseEnd : 200
427432 }
428433
434+ mockNavigationEntry = { name : 'https://example.com/' }
435+
429436 global . performance . getEntriesByType = jest . fn ( ( type ) => {
430- if ( type === 'navigation' ) {
431- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
432- }
433437 if ( type === 'resource' ) {
434438 return [ mockResourceEntry ]
435439 }
@@ -458,10 +462,9 @@ describe('script-tracker', () => {
458462 responseEnd : 200
459463 }
460464
465+ mockNavigationEntry = { name : 'https://example.com/' }
466+
461467 global . performance . getEntriesByType = jest . fn ( ( type ) => {
462- if ( type === 'navigation' ) {
463- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
464- }
465468 if ( type === 'resource' ) {
466469 return [ mockResourceEntry ]
467470 }
@@ -487,10 +490,9 @@ describe('script-tracker', () => {
487490 responseEnd : 180.6
488491 }
489492
493+ mockNavigationEntry = { name : 'https://example.com/' }
494+
490495 global . performance . getEntriesByType = jest . fn ( ( type ) => {
491- if ( type === 'navigation' ) {
492- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
493- }
494496 if ( type === 'resource' ) {
495497 return [ mockResourceEntry ]
496498 }
@@ -519,10 +521,9 @@ init@https://cdn.example.com/gecko-app.js:20:10`
519521 responseEnd : 210.4
520522 }
521523
524+ mockNavigationEntry = { name : 'https://example.com/' }
525+
522526 global . performance . getEntriesByType = jest . fn ( ( type ) => {
523- if ( type === 'navigation' ) {
524- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
525- }
526527 if ( type === 'resource' ) {
527528 return [ mockResourceEntry ]
528529 }
@@ -552,10 +553,9 @@ init@https://cdn.example.com/gecko-app.js:20:10`
552553 responseEnd : 150
553554 }
554555
556+ mockNavigationEntry = { name : 'https://example.com/' }
557+
555558 global . performance . getEntriesByType = jest . fn ( ( type ) => {
556- if ( type === 'navigation' ) {
557- return [ { initiatorType : 'navigation' , name : 'https://example.com/' } ]
558- }
559559 if ( type === 'resource' ) {
560560 return [ mockResourceEntry ]
561561 }
0 commit comments