happy-dom icon indicating copy to clipboard operation
happy-dom copied to clipboard

new features of Node.js: native fetch

Open Mas0nShi opened this issue 2 years ago • 6 comments

node add the experimental fetch after v17.5.0, so maybe we can delete package dependency of node-fetch in the near future.

Refer: https://nodejs.org/en/blog/release/v17.5.0

Mas0nShi avatar Jun 10 '22 12:06 Mas0nShi

I also read about it. It would be nice to use native 🙂 I think we need to wait until it is available in an LTS version.

capricorn86 avatar Jun 14 '22 10:06 capricorn86

Node 18 LTS started this week and fetch is available without flags, but its classified as experimental

https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch

ypconstante avatar Oct 27 '22 13:10 ypconstante

Nice 🤩

Mas0nShi avatar Oct 27 '22 14:10 Mas0nShi

I notice that my tests that use happy-dom fail when I compare Request objects in node 18 but they don't fail in node 16. I'd love to use node 18 and the internal WHATWG exports with happy-dom soon. In the meantime, I am going to update my tests to break down a Request object into the pieces I care about.

tatemz avatar Apr 21 '23 14:04 tatemz

Happy DOM has its own implementation of Request, Response, fetch() etc., as it has dependencies to internal logic of Happy DOM which the native fetch() doesn't have as its not a DOM. For example will Happy DOM set cookies in the document if a response has "Set-Cookie" headers.

@tatemz do you have an example of when it fails?

capricorn86 avatar Apr 21 '23 15:04 capricorn86

I was able to override it like this:

import { GlobalRegistrator } from "@happy-dom/global-registrator";

const f = fetch;

GlobalRegistrator.register();

window.fetch = f;

This fixed an issue for me with the implemented fetch shim:

image

https://github.com/capricorn86/happy-dom/blob/0f0be8e4879ce564047f9434ddf1cd0fa06eaee7/packages/happy-dom/src/fetch/Fetch.ts#L399

The issue happens every time using bun test. It also happens for script tags with Bun JS and then this workaround doesn't work.

See the same bug filed on their tracker: https://github.com/oven-sh/bun/issues/8774

(Sorry for not debugging more. I'm already spending time I don't have trying to migrate my tests)

friday avatar Jan 30 '24 23:01 friday