I am making a request from a react app using a relative path "/api/test". (an NGINX controller routes traffic to either the api or the react client, so they have the same base url)
I found in the msw documentation that relative paths are supported.
// Given your application runs on "http://localhost:8080",
// this request handler URL gets resolved to "http://localhost:8080/invoices"
rest.get('/invoices', invoicesResolver)
Note that relative URL are resolved against the current location (location.origin).
If I console.log(window.location.origin)
, it prints http://localhost:3000
. I think that the origin is being set because of import '@testing-library/jest-dom'
.
However, if I make a fetch call during testing (msw is mocking api calls), I get a TypeError: Invalid URL: /api/test
.
fetch('/api/test')
.then((response) => response.json())
.then((data) => console.log(data))
Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
TypeError: Failed to parse URL from /api/test
❯ new Request node:internal/deps/undici/undici:9474: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
❯ globalThis.fetch node_modules/@mswjs/interceptors/src/interceptors/fetch/index.ts:41:42
❯ App src/App.tsx:9:3
7|
8| function App() {
9| fetch('/api/test')
| ^
10| .then((response) => response.json())
11| .then((data) => console.log(data))
❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:16305:18
This error originated in "src/App.test.tsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
Caused by: TypeError: Invalid URL: /api/test
❯ new URLImpl node_modules/whatwg-url/lib/URL-impl.js:21:13
❯ Object.exports.setup node_modules/whatwg-url/lib/URL.js:54:12
❯ new URL node_modules/whatwg-url/lib/URL.js:115:22
❯ new Request node:internal/deps/undici/undici:9472:25
❯ 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
When I run the code in the browser, there are no issues. Given that document.baseURI
and window.location.origin
are set, how does fetch "know" it is not running in a browser? Is it possible to avoid this error by setting another field in window
?