happy-dom
happy-dom copied to clipboard
new features of Node.js: native fetch
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
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.
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
Nice 🤩
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.
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?
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:
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)