question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unexpected behaviours between MockAgent and node's bundled fetch

See original GitHub issue

Bug 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:closed
  • Created a year ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
KhafraDevcommented, May 22, 2022

Yep, using global symbols (idk the correct term; using Symbol.for instead of Symbol) fixes the issues, although it would be great to remove the symbols altogether. I had two ideas regarding removing the symbol:

  • Async mocking would allow us to resolve the async iterable body instead of using request.body.source.
  • Check if the current agent is a MockAgent instead. Wouldn’t work; mocks could be active/inactive, still need to check the symbol.

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 on MockAgent that returns this[kIsMockActive] and everything seems to work.

0reactions
mcollinacommented, May 22, 2022

go for the updated solution, I’ll ship a release early next week.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found