mswjs / examples

Examples of Mock Service Worker usage with various frameworks and libraries.

Home Page:https://mswjs.io/examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use absolute url in the api functions and msw handlers

exaucae opened this issue · comments

One should not use /login but an absolute url as states ref doc.

@chrys-exaucet Can you point out what/where exactly you're referring to here that doesn't work as expected? Relative paths are valid in browser environments and without any further direction as to where the error is, we can't fix it. Any help is appreciated, thank you!

I surprisingly came accross an error regarding the relative path here and there while implementing msw in my React app, and only fixed it with absolute ones.
I'll write down the reproducable mistake by the end of the day.

Regards.

I surprisingly came accross an error regarding the relative path here and there while implementing msw in my React app, and only fixed it with absolute ones.
I'll write down the reproducable mistake by the end of the day.

Regards.

Was it in a test by any chance? If so, that could explain it. Otherwise, looking forward to seeing the repro - and thank you for the prompt reply!

Yes it was. Would mind elaborating?
I may change the issue title for accuracy purpose.

@chrys-exaucet the reason why you're getting the "Only absolute URL are supported" error is, most likely, because you're not using JSDOM, or similar polyfill for Node.js. In Node.js there's no such thing as a relative URL. How most of the test frameworks function to allow testing your code that does relative requests, is by stubbing the window and specifically its window.location property that is treated as a base URL for any relative ones.

The documentation and the examples are correct, using relative URLs is perfectly fine. If you do, it's your responsibility to ensure that the testing environment supports relative URLs. Jest does that by default, but I can imagine that some additional setup may be necessary for other testing frameworks.