11const dns = require ( 'node:dns' ) ;
2- const { MockAgent, setGlobalDispatcher } = require ( 'undici' ) ;
2+ const { fetch : undiciFetch , MockAgent, setGlobalDispatcher } = require ( 'undici' ) ;
33
44const staticIndex = require ( '../../test/data/static/index.json' ) ;
55const staticSchedule = require ( '../../test/data/static/schedule.json' ) ;
@@ -8,11 +8,16 @@ function beforeEachTemplate() {
88 // this fixes a bug in Node.js - it should be fixed _eventually_ and can be removed: https://github.com/nodejs/undici/issues/1248
99 dns . setDefaultResultOrder ( 'ipv4first' ) ;
1010
11- // this mock agent stuff isn't actually working for... some unkown reason
1211 const mockAgent = new MockAgent ( ) ;
1312 mockAgent . disableNetConnect ( ) ;
1413 setGlobalDispatcher ( mockAgent ) ;
1514
15+ // Node.js's built-in globalThis.fetch uses a bundled copy of undici separate from
16+ // the npm package, so setGlobalDispatcher above doesn't affect it. Replacing it here
17+ // ensures all fetch calls (including those that fall through to globalThis.fetch in
18+ // optionsParser) go through the mock dispatcher.
19+ globalThis . fetch = undiciFetch ;
20+
1621 const defaultIndexMock = mockAgent . get ( 'https://nodejs.org' ) ;
1722 defaultIndexMock
1823 . intercept ( { path : '/dist/index.json' } )
@@ -25,13 +30,11 @@ function beforeEachTemplate() {
2530 . intercept ( { path : '/nodejs/Release/master/schedule.json' } )
2631 . reply ( 200 , staticSchedule ) ;
2732
28- const customIndexMock = mockAgent . get ( 'https://bnb.im' ) ;
29- customIndexMock
33+ const customMock = mockAgent . get ( 'https://bnb.im' ) ;
34+ customMock
3035 . intercept ( { path : '/dist/index.json' } )
3136 . reply ( 200 , staticIndex ) ;
32-
33- const customScheduleMock = mockAgent . get ( 'https://bnb.im' ) ;
34- customScheduleMock
37+ customMock
3538 . intercept ( { path : '/dist/schedule.json' } )
3639 . reply ( 200 , staticSchedule ) ;
3740}
0 commit comments