Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 796a413

Browse files
committedMar 28, 2024
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: nodejs#52015
1 parent 61e5de1 commit 796a413

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed
 

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

+6-17
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,16 @@ installObjectURLMethods();
5959

6060
{
6161
// 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-
}
6962
ObjectDefineProperty(globalThis, 'fetch', {
7063
__proto__: null,
7164
configurable: true,
7265
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;
66+
writable: true,
67+
value: function fetch(input, init = undefined) { // eslint-disable-line func-name-matching
68+
// Loading undici alone lead to promises which breaks lots of tests so we
69+
// have to load it really lazily for now.
70+
const { fetch: impl } = require('internal/deps/undici/undici');
71+
return impl(input, init);
8372
},
8473
});
8574
}

0 commit comments

Comments
 (0)
Please sign in to comment.