unexpected behaviours between MockAgent and node's bundled fetch
See original GitHub issueBug Description
Intercepting undici’s own fetch body works but node’s doesn’t. This is building on top of the global global dispatcher shipped with 5.2.0 which is then used by Node 18.2.0 bundled fetch.
Reproducible By
import * as undici from 'undici' // 5.2.0
const mockAgent = new undici.MockAgent()
mockAgent.disableNetConnect()
undici.setGlobalDispatcher(mockAgent)
const pool = mockAgent.get('https://example.com')
pool.intercept({
path: '/path',
method: 'POST',
body(body) {
console.log(body)
return true
}
}).reply(200, { ok: 1 }).times(2)
const url = new URL('https://example.com/path').href
// undici fetch v5.2.0 from node_modules
await undici.fetch(url, {
method: 'POST',
body: JSON.stringify({ foo: 'bar' })
})
// undici fetch v5.2.0 from node 18.2.0 global
await globalThis.fetch(url, {
method: 'POST',
body: JSON.stringify({ foo: 'bar' })
})
The above logs
{"foo":"bar"} // undici fetch v5.2.0 from node_modules
Object [AsyncGenerator] {} // undici fetch v5.2.0 from node 18.2.0 global
Expected Behavior
It is expected that the string body is passed into the matching function both times.
Environment
Node.js 18.2.0 Undici 5.2.0
Issue Analytics
- State:
- Created a year ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
API Docs | fetch-mock - Wheresrhys
fetch -mock allows mocking http requests made using fetch or a library imitating its api, such as node-fetch or fetch-ponyfill.
Read more >platform/frameworks/base - Git at Google
Merge "Clean up some javadoc issues" into nyc-dev diff --git a/api/current.txt b/api/current.txt index 4c069cd..38d2a03 100644 --- a/api/current.txt +++ ...
Read more >Node.js Undici
For more information about their behavior, please reference the body mixin from the Fetch Standard. Common API Methods. This section documents our most...
Read more >(PDF) Tracking Causality by Visualization of Multi-Agent ...
We redefine the concept of causality graph for this particular field and propose an algorithm for generation of such a graph. Causality ...
Read more >Using Estimation of Distribution Algorithms to Detect ...
faults result from unforeseen timings of interactions between ... nicate, and therefore makes it easier to predict the behaviour of a system.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yep, using global symbols (idk the correct term; using
Symbol.for
instead ofSymbol
) fixes the issues, although it would be great to remove the symbols altogether. I had two ideas regarding removing the symbol:request.body.source
.Check if the current agent is aWouldn’t work; mocks could be active/inactive, still need to check the symbol.MockAgent
instead.I haven’t gotten around to working on either, though. I have a PR that changes the is mock active symbol to using
Symbol.for
but I haven’t written tests (no idea how to) and I haven’t checked if mock symbols are used elsewhere in fetch where it’d also cause issues.edit: I found a solution without modifying the symbols - just need to make an
active
getter onMockAgent
that returnsthis[kIsMockActive]
and everything seems to work.go for the updated solution, I’ll ship a release early next week.