msw: Node interceptors don't resolve relative URLs when "location" is present
Prerequisites
- I confirm my issue is not in the opened issues
- I confirm the Frequently Asked Questions didn’t contain the answer to my issue
Environment check
- I’m using the latest
mswversion - I’m using Node.js version 14 or higher
Node.js version
v20.1.0
Reproduction repository
https://github.com/allevo/mock-backend-apis-reactjs
Reproduction steps
cd react-with-msw
npm ci
npm test
Current behavior
TypeError: Failed to parse URL from /api/movies
❯ new Request node:internal/deps/undici/undici:7084:19
❯ FetchInterceptor.<anonymous> node_modules/@mswjs/interceptors/src/interceptors/fetch/index.ts:42:23
❯ step node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:59:23
❯ Object.next node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:40:53
❯ node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:34:71
❯ __awaiter node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js:30:12
❯ fetch node_modules/@mswjs/interceptors/src/interceptors/fetch/index.ts:41:42
❯ loadMovies src/App.tsx:18:12
16| function loadMovies() {
17| setMovies(undefined)
18| return fetch('/api/movies')
| ^
19| .then(res => res.json())
20| .then(movies => setMovies(movies))
Expected behavior
/api/movies should be mocked.
I followed this article to configure the repo https://kentcdodds.com/blog/stop-mocking-fetch
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 11
- Comments: 20 (7 by maintainers)
Commits related to this issue
- test: test with msw failed https://github.com/mswjs/msw/issues/1625 — committed to takami-h/fizzbuzz-app by takami-h 8 months ago
I had a similar experience to the others trying to add MSW to an existing vitest/vue app.
As already mentioned by @hwride, the call to the Request constructor errors in https://github.com/mswjs/interceptors/blob/cf4e2284f4d2d5f6862b6a458148dff224f65038/src/interceptors/fetch/index.ts#L34
As the Request constructor in my case was the one from undici, I added undici as a dev dependency and set the global origin to
window.location.hrefbefore each test (well, running it once would’ve worked as well since in our case the url does not change but better safe than sorry 😉My code is roughly (I left out the part where I work around 946)
And in vitest.config.ts
To test:
This is quite a blocker. Modifying fetch call side is not a real solution.
I am also experiencing this issue in my test setup. The issue seems to be on the MSW side. It is not resolving urls that are being called in the client-side code, even though the
locationglobal is set properly in the JSDom environment. MSW doesn’t seem to be resolving the relative urls at all. It doesn’t seem to recognize the relative URLs that are being used in the client-side code.FYI - Here’s an Minimal Complete Verifiable Example (MCVE). In this case, jsdom is loaded via pragma and adds
window.locationto the environment, but fetch still requires the absolute URLAnd you can fix the tests by including the origin in the url
@LuanScudeler, that can certainly work as well! However, it’s okay to request relative resources in the client-side code. I don’t believe that needs any adjustments. The pickle here is configuring the test environment, which is Node.js, where relative URLs do not exist.
This may as well be a bug on our side, we should pick up and respect the
locationglobal. We do so in the source code but, apparently, there are scenarios when this doesn’t work as expected:https://github.com/mswjs/interceptors/blob/ca47c880790cabab58e667d1a81a82d25ab31a33/src/interceptors/fetch/index.ts#L51-L54
Someone would have to look into the reported reproduction repository and see what’s going on.
So for my side, the error is throw before it gets to that line, at the
Requestconstructor:See here: https://github.com/mswjs/interceptors/blob/ca47c880790cabab58e667d1a81a82d25ab31a33/src/interceptors/fetch/index.ts#L42
Where my fetch call is:
fetch('/api/signup')Possibly the fix is moving the
locationbased URL normalising above thenew Requestcall, and passing the normalised URL tonew Requestas well as tonew IsomorphicRequest, where as at the moment it’s only passed tonew IsomorphicRequest. Something like:Hi, I’m bit confused about this topic. I was having the same issue as @allevo, but I’ve solved it by adding the hostname to the actual Fetch API call and not in the MSW request handler call, so now the working setup I have is something like this:
Doesn’t this means that MSW request handler is fine with relative URLs and then something else is not resolving relative URLs? Maybe node-fetch?
Hi! Thanks again for your patience. The test
prints correctly
So don’t understand where is the mistake