mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
2cd3073e0f
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: https://github.com/nodejs/node/issues/52015 PR-URL: https://github.com/nodejs/node/pull/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>
21 lines
482 B
JavaScript
21 lines
482 B
JavaScript
'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();
|
|
});
|