-
| Hi everyone, I’m migrating to ky in my project and facing an issue while testing in a Jest environment. In the browser, everything works perfectly fine with relative URLs, but my tests fail with the following error: Setup: 
 Problem: Relative URLs (e.g., /example/api) work fine in the browser but fail in the Jest environment. I understand that this might be because Node.js (or jsdom) lacks a document.baseURI, which the browser naturally provides for resolving relative URLs. The thing is that once I replace  Any help, advice, or best practices would be greatly appreciated! 😊 | 
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
| I have fixed this by providing the following  export const apiClient = ky.extend({
    ...otherOptions,
    prefixUrl: location.origin,
});This does mean you have to remove the  | 
Beta Was this translation helpful? Give feedback.
-
| Interesting that it works with  If it does, then it means jsdom is not properly implementing  As an aside, I would strongly recommend using Playwright instead of jsdom. It's a much more realistic test environment and still very fast and easy to set up. | 
Beta Was this translation helpful? Give feedback.
Interesting that it works with
fetch. I suspect this is happening because of how jsdom implementsRequest, which we use before passing the input on tofetch. Can you try replacingfetch()withnew Request()and see if that triggers the error you have been seeing?If it does, then it means jsdom is not properly implementing
Request, because it's supposed to use the base URL /location.originif available.As an aside, I would strongly recommend using Playwright instead of jsdom. It's a much more realistic test environment and still very fast and easy to set up.