Skip to content
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 does not accept a request object in first parameter #1433

Closed
jasonkuhrt opened this issue May 12, 2024 · 6 comments · Fixed by #1590
Closed

Fetch does not accept a request object in first parameter #1433

jasonkuhrt opened this issue May 12, 2024 · 6 comments · Fixed by #1590
Assignees
Labels
bug Something isn't working

Comments

@jasonkuhrt
Copy link

jasonkuhrt commented May 12, 2024

Describe the bug

When trying to pass a request object to fetch the following error happens:

TypeError: Failed to parse URL from [object Request]

To Reproduce

const requestObject = new Request(input.url, {
    method: `POST`,
    body: '',
  })

  const response = await fetch(requestObject)

Expected behavior
Should accept request object in first parameter.

Other

It works with jsdom.

@kettanaito
Copy link

Some of MSW community members also encountering this: mswjs/msw#2191.

@capricorn86
Copy link
Owner

Thank you for reporting @jasonkuhrt and @kettanaito! 🙂

This should be supported already in Happy DOM.

I have added an additional unit test to cover this now here:
https://github.com/capricorn86/happy-dom/blob/master/packages/happy-dom/test/fetch/Fetch.test.ts#L270

Is it something that I'm missing? Is it possible to setup a reproducible example project somewhere?

@capricorn86
Copy link
Owner

By using the example project in mswjs/msw#2191, I managed to find the problem.

This is a bug in Vitest, where Vitest is using the native fetch from Node.js and not the one from Happy DOM. It is using the Request and Response objects from Happy DOM. As the Request object is not recognizable by the native fetch, it will try to convert it to a string.

@capricorn86
Copy link
Owner

Bug created for Vitest:
vitest-dev/vitest#6878

@kettanaito
Copy link

@capricorn86, outstanding work. Thank you so much!

@capricorn86
Copy link
Owner

The fix has been released now in Vitest. I'm closing this one now. Please re-open if the problem still exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants