forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 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
Showing
2 changed files
with
35 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |