Skip to content

Commit 57f67a1

Browse files
committed
test: fix beforeEachTemplate's fetch implementation
Signed-off-by: Tierney Cyren <hello@bnb.im>
1 parent ffe9706 commit 57f67a1

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

core/util/dev/beforeEachTemplate.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const dns = require('node:dns');
2-
const { MockAgent, setGlobalDispatcher } = require('undici');
2+
const { fetch: undiciFetch, MockAgent, setGlobalDispatcher } = require('undici');
33

44
const staticIndex = require('../../test/data/static/index.json');
55
const 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

Comments
 (0)