-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Constructing a Request for a relative URL fails #4434
Comments
Start a new pull request in StackBlitz Codeflow. |
This is just how node works: > node
new Request('/api/foo')
// TypeError: Invalid URL |
Sure. But using a browser-like environment in my tests should behave as a browser, not like node?! |
You don't use any environment in your reproduction. If you are using any environment, then Vitest doesn't control what variables are exposed and how they work. |
My bad. I updated the reproduction to represent more my setup. And btw using the following solves my current issue:
|
Currently I try to migrate from the cra stack to vite and vitest. The app I build relies on the browser's behavior to preceed a relative URL since this makes the app independent from a certain URL. In contrast to @micha149 I'm using jsdom. There one can provide an own Unfortunately, it seems that vitest serializes and deserializes the custom resource loader when passing that option to jsdom:
The next approach I try to follow is to create a custom environment. |
I fixed the issue with happy-dom, but jsdom doesn't support Request/Response, so I would recommend refactoring your code to something like this to support all environments: new Request(new URL('/api/foo', import.meta.url)) |
Describe the bug
I'm using vitest with happy-dom. In my application code I want to instanciate a Request from a relative URL like
new Request('/api/foo')
. From my perspective this should work and in my browser all works fine as expected. But creating the instance im my tests fails with:I digged a bit into the problem but can't really figure out whats going wrong. Something I noticed is that there is the
Request
constructor fromnode:internal
instead ofhappy-dom
when I print a stack trace from the URL constructor:Switching to
jsdom
does not solve the issue. As well as creating a standalone script which useshappy-dom
to create a Request works fine. So I think the problem lies in the integration into vitest.Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-8gdvjz?file=test%2Frequest.test.ts
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: