-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fetch
function causes a weird error with bun test + react + happy-dom
#8774
Comments
This workaround works for me. import { GlobalRegistrator } from "@happy-dom/global-registrator";
const bunFetch = fetch;
GlobalRegistrator.register();
window.fetch = bunFetch; But it will not work when happy-dom is evaluating script tags. I found that wrapping the request method worked for that though (I'm confused about why because I think the import { GlobalRegistrator } from "@happy-dom/global-registrator";
import HTTP, { request as HTTPRequest } from "http";
import HTTPS, { request as HTTPSRequest } from "https";
type RequestParams = Parameters<typeof HTTPRequest>;
HTTP.request = function (...args: any[]) {
return Object.assign(HTTPRequest(...(args as RequestParams)), {
end() {},
});
};
HTTPS.request = function (...args: any[]) {
return Object.assign(HTTPSRequest(...(args as RequestParams)), {
end() {},
});
};
GlobalRegistrator.register(); You can also disable evaluating script tags: GlobalRegistrator.register({
settings: {
disableJavaScriptEvaluation: true,
disableJavaScriptFileLoading: true,
},
}); But it will just produce another error instead |
@friday thanks a lot for your replies. Your solutions kinda help, but, in my case, there's still some issues that neither solve actually handles completely. With the approach to override the I tried using other fetch libraries for testing, but, I just couldn't get it to work properly. Have you faced such issue? |
I also just bumped into this and not sure what to do :( My thought is to make my own custom fetch wrapper that uses the Node API to do http requests instead of Fetch (Nope, Happy-Dom overwrites that too!), and then on browser use Fetch so it's isomorphic but feels like busy work hmm. Edit a week later: Hmm... I attempted to look into this more. After trial and error, I did this in my preload:
Then in tests I want to opt-out, like API testing.... I did:
I was trying to setup happy-dom in the react tests only but it complained "For queries bound to document.body a global document has to"... For some reason I could only get working if in my preload... It feels hacky but it works if calling the test script directly. Hover if doing Then most of my React code is pure functions as not too far yet, so not using Fetch in them yet. Just was testing my API at this point.... but I think maybe I'd just end up mocking fetch( or a similar endpoint for my components where I want to grab data, but a lot will just be dealing with pure data passed in by another one I believe. Hopefully my comment can help others looking for a workaround. |
Fixed in Bun v1.1.21 ❯ bun-1.1.21 test --preload=./happydom.ts
bun test v1.1.21 (70ca2b76)
src/App.test.js:
✓ Home button renders [9.43ms]
1 pass
0 fail
1 expect() calls
Ran 1 tests across 1 files. [134.00ms] (with the App.js file modified to have a "Home" button) |
What version of Bun is running?
1.0.26+c75e768a6
What platform is your computer?
Darwin 23.3.0 arm64 arm
What steps can reproduce the bug?
bun i
bun test
What is the expected behavior?
An error telling me
fetch
is not mockedWhat do you see instead?
Additional information
Sorry if this is a not-so-good issue, i'm tired. I need a nap.
The text was updated successfully, but these errors were encountered: