Skip to content

Commit

Permalink
lib: refactor lazy loading of undici for fetch method
Browse files Browse the repository at this point in the history
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
PR-URL: nodejs#52275
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
Victor Chen authored Apr 12, 2024
1 parent 1091efc commit 2cd3073
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
41 changes: 15 additions & 26 deletions lib/internal/bootstrap/web/exposed-window-or-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,21 @@ defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
const { installObjectURLMethods } = require('internal/url');
installObjectURLMethods();

{
// https://fetch.spec.whatwg.org/#fetch-method
function set(value) {
ObjectDefineProperty(globalThis, 'fetch', {
__proto__: null,
writable: true,
value,
});
}
ObjectDefineProperty(globalThis, 'fetch', {
__proto__: null,
configurable: true,
enumerable: true,
set,
get() {
function fetch(input, init = undefined) {
// Loading undici alone lead to promises which breaks lots of tests so we
// have to load it really lazily for now.
const { fetch: impl } = require('internal/deps/undici/undici');
return impl(input, init);
}
set(fetch);
return fetch;
},
});
}
let fetchImpl;
// https://fetch.spec.whatwg.org/#fetch-method
ObjectDefineProperty(globalThis, 'fetch', {
__proto__: null,
configurable: true,
enumerable: true,
writable: true,
value: function value(input, init = undefined) {
if (!fetchImpl) { // Implement lazy loading of undici module for fetch function
const undiciModule = require('internal/deps/undici/undici');
fetchImpl = undiciModule.fetch;
}
return fetchImpl(input, init);
},
});

// https://xhr.spec.whatwg.org/#interface-formdata
// https://fetch.spec.whatwg.org/#headers-class
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-fetch-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
require('../common');
const { mock, test } = require('node:test');
const assert = require('node:assert');

test('should correctly stub globalThis.fetch', async () => {
const customFetch = async (url) => {
return {
text: async () => 'foo',
};
};

mock.method(globalThis, 'fetch', customFetch);

const response = await globalThis.fetch('some-url');
const text = await response.text();

assert.strictEqual(text, 'foo');
mock.restoreAll();
});

0 comments on commit 2cd3073

Please sign in to comment.