Skip to content

Commit 2cd3073

Browse files
author
Victor Chen
authored
lib: refactor lazy loading of undici for fetch method
Object.defineProperty is updated to lazily load the undici dependency for the fetch method. This change allows for simpler and more reliable mocking of the fetch method for testing purposes, resolving issues encountered with premature method invocation during testing. Fixes: #52015 PR-URL: #52275 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 1091efc commit 2cd3073

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

lib/internal/bootstrap/web/exposed-window-or-worker.js

+15-26
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,21 @@ defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
5757
const { installObjectURLMethods } = require('internal/url');
5858
installObjectURLMethods();
5959

60-
{
61-
// https://fetch.spec.whatwg.org/#fetch-method
62-
function set(value) {
63-
ObjectDefineProperty(globalThis, 'fetch', {
64-
__proto__: null,
65-
writable: true,
66-
value,
67-
});
68-
}
69-
ObjectDefineProperty(globalThis, 'fetch', {
70-
__proto__: null,
71-
configurable: true,
72-
enumerable: true,
73-
set,
74-
get() {
75-
function fetch(input, init = undefined) {
76-
// Loading undici alone lead to promises which breaks lots of tests so we
77-
// have to load it really lazily for now.
78-
const { fetch: impl } = require('internal/deps/undici/undici');
79-
return impl(input, init);
80-
}
81-
set(fetch);
82-
return fetch;
83-
},
84-
});
85-
}
60+
let fetchImpl;
61+
// https://fetch.spec.whatwg.org/#fetch-method
62+
ObjectDefineProperty(globalThis, 'fetch', {
63+
__proto__: null,
64+
configurable: true,
65+
enumerable: true,
66+
writable: true,
67+
value: function value(input, init = undefined) {
68+
if (!fetchImpl) { // Implement lazy loading of undici module for fetch function
69+
const undiciModule = require('internal/deps/undici/undici');
70+
fetchImpl = undiciModule.fetch;
71+
}
72+
return fetchImpl(input, init);
73+
},
74+
});
8675

8776
// https://xhr.spec.whatwg.org/#interface-formdata
8877
// https://fetch.spec.whatwg.org/#headers-class

test/parallel/test-fetch-mock.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
require('../common');
3+
const { mock, test } = require('node:test');
4+
const assert = require('node:assert');
5+
6+
test('should correctly stub globalThis.fetch', async () => {
7+
const customFetch = async (url) => {
8+
return {
9+
text: async () => 'foo',
10+
};
11+
};
12+
13+
mock.method(globalThis, 'fetch', customFetch);
14+
15+
const response = await globalThis.fetch('some-url');
16+
const text = await response.text();
17+
18+
assert.strictEqual(text, 'foo');
19+
mock.restoreAll();
20+
});

0 commit comments

Comments
 (0)