Replies: 5 comments 3 replies
-
Hey, @srcreigh. Thank you for your kind feedback! We do mention that relative URLs are resolved against
If your backend runs on a separate host/port than your frontend, it's implied that you need to construct an absolute URL. We come from the same general expectations when working with // You don't expect this to request your backend
// because you understand "/data" resolves against
// the current hostname (frontend).
fetch('/data')
// So to fetch data from your backend
// you provide an absolute URL to it.
// (Which may contain env variables, it's fine).
fetch('http://localhost:5000/data') And so request matching in MSW works the same way in regards to relative/absolute URLs. We do provide smart handler suggestions on unhandled requests. For example, given your use case you'd see the following warning in the browser's console/test output:
We don't perform any kind of diff'ing, as the suggestion above is enough for you to sport the problem (in your case a different port). If you don't see this warning message, this likely means you're using an older version of the library. Update to the latest version of MSW to get the best experience. Regarding a loose matching of relative and absolute URL, I don't think that ignoring the base URL is a good idea. Coming back to the common expectations when working with any request client that I've described above, people don't expect the base URL to be ignored. This kind of implicitness can be hurtful and raise a lot of problems. You can ignore the base URL explicitly by using a path string with a wildcard: rest.get('*/data', resolver) I wouldn't commend that, as the nature of endpoints is largely static (even dynamic base URLs based on the environment still produce a static URL in the end), which means the URL cannot suddenly change on runtime. Including an explicit base URL, no matter if conditional, is a good approach to take. // Look, this intercepts a request to an absolute URL
// using this particular env variable as a base URL.
rest.get(`{process.env.BASE_URL}`/data`, resolver) Generally, prefer explicitness. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the detailed response. The wildcard matching is really great! If it wasn't clear before. One of my hoped for outcomes was to have slightly more information about the wonderful request matching system directly in the getting started guide. In my personal opinion, it would be nice for the request matching to "just work" without having to debug or read separate documentation page. My experience was like this: YES /login will match my backend! Oh wait it doesn't... hmm why? double checks code... confusion and maybe a little frustration... scans through documentation... reads about request matching... Oh this makes a lot of sense... fixes the code I got from the getting started guide... works perfectly now!. Specifically this part: https://mswjs.io/docs/getting-started/mocks/rest-api#request-handler Here's a few ideas:
I think a little extra info there might help new folks understand just how well designed this library is 😁 We can't just go around expecting this high level of quality everywhere, help us out a bit haha.
Respectfully, in my noob state given only knowledge from what I read in the getting started guide, it was not enough. My mental model at that point was that "/login" does wildcard matching - but it's not matching - is the library broken or something? Of course it wasn't, it's great, but that was my experience. Anyways, I defer to you. Thanks again for the wonderful library and i'm so excited to be using such a simple and great tech 😁 |
Beta Was this translation helpful? Give feedback.
-
Yes! Please tag me on anything. Are the docs generated from this repo? Happy to send a PR, or we can work through the addition in this issue. (Forgive me for replying so late - I must've archived the email half awake.) |
Beta Was this translation helpful? Give feedback.
-
I just wanted to chime in that I was also bitten by this a little. I did find the documentation eventually which mentioned relative paths are relative to the page's hostname, but I then spent a while trying to find an option to set (maybe in |
Beta Was this translation helpful? Give feedback.
-
Hi. I wanted to share that I struggled with this as well. In my case I was mocking the At the end I setup the test in another way that you don't need to mock and it worked fine, but wanted to share. From MSW side, maybe documentation can be improved to include this |
Beta Was this translation helpful? Give feedback.
-
Hello, thank you SO MUCH for this wonderful library. I'm very excited to use it. I am so excited the potential of mocking the network layer instead of individual functions.
I want to let you know that I was very confused why this setup code didn't match requests to my development server:
The reason why is that I my development backend server runs on a different port than my react dev server. I have to imagine that this setup is very common; although my current workplace serves React from a local Rails server, every other previous workplace and in all my personal projects I used http://localhost:3000 for React and http://localhost:5000 or something for the backend (ie separate ports/processes). Why? Setting up the back-end to serve the React app is an extra setup step which I can't currently justify.. nginx reverse proxy and 2 servers in prod is a simple way to not complicate the API server.. I'm also not sure if serving react via the API server locally will have the nice live reloading experience that the
npm start
create-react-app dev server provides.(Please note that before posting this issue, I inspected all open issues as well as also several closed issue threads containing the word "relative", for relative paths. Unless I missed something, I have some confirmation that this issue hasn't been addressed.)
What follows is a few brainstormed ideas to help with clarity of others regarding relative path matching:
rest.post($YOUR_API_ROOT_PATH + '/login'
, with a tip saying "If you backend and frontend use the same port, you can shorten this torest.post('/login')
!" That way, the default works and there's better options for some people, vs the default doesn't work for some people but works for others./login
should match$DIFFERENT_HOST/login
, in addition to$SAME_HOST/login
. Yes, maybe this will match two different hosts to a single handler, which isn't ideal... ehhh... I don't want to go down too far the rabbit hole of suggesting potentially backwards incompatible changes so I'll stop there. :)To reiterate on the main point - I'm so happy that this library exists.. it was what I opted to spend my evening trying out after a day of research! Btw, I found out about it since the 3rd Google search result for "how to mock fetch in jest" is the Kent C Dodds article recommending msw. Anyways, I am a big fan and thank you so much for all your work til now. Cheers 😁
Beta Was this translation helpful? Give feedback.
All reactions