Msw: Empty URL in resolver.

Created on 23 Oct 2020  路  20Comments  路  Source: mswjs/msw

Describe the bug

I spent a lot of time to get why my url don't mock. The reason is empty URL.

image

im sending request to /dev/api/v1/auth/login but console.log(req.url) show in terminal empty URL {} object:

image

Environment

  • msw: 0.21.3
  • nodejs: 14.14.0
  • npm: 6.14.18

Please also provide your browser version.
Im using JEST tests. There is no browser running.

rest bug discussion node

Most helpful comment

You don't need MSW update: it specifies node-request-interceptor dependency version range permissive enough to pull in a patched version release:

https://github.com/mswjs/msw/blob/327b384938b1fdf44e77c5bd79e91b7fab0aaad4/package.json#L73

"^0.5.1" will install "0.5.1" as well as "0.5.3" ("0.5.x")

Neither you should install that 4th party dependency manually. Please reinstall your dependency and the new version of node-request-interceptor will be automatically installed. You can verify that by running the following command after the installation:

$ npm ls node-request-interceptor

It works! Thank you! I close issue.

All 20 comments

Hi @olegKusov thanks for reaching us :).

The req.url is an instance of URL. You can access the host with req.url.hostwhile the path with req.url.pathname. Let me know if this will solve your issue :)

Hi @olegKusov thanks for reaching us :).

The req.url is an instance of URL. You can access the host with req.url.hostwhile the path with req.url.pathname. Let me know if this will solve your issue :)

yes.host is undefined and pathname right. But i don't understand why my pattern doesnt match. I pasted path from pathname

image

Are you using MSW in a DOM-like environment ? If not it will not work, you should absolute url in this case

https://mswjs.io/docs/getting-started/integrate/node#direct-usage

Hey, @olegKusov. Can you please try the onUnhandledRequest option of your server to see what is the actual request, and why they don't match?

const server = setupServer(...)
server.listen({
  onUnhandledRequest: 'warn'
})

Read about it in the docs

Could you also provide how do you perform an actual request?

Most of the mismatch cases are due to a typo, wrong method, or relative URL in a NodeJS environment.

Hey, @olegKusov. Can you please try the onUnhandledRequest option of your server to see what is the actual request, and why they don't match?

const server = setupServer(...)
server.listen({
  onUnhandledRequest: 'warn'
})

Read about it in the docs

image

Are you using MSW in a DOM-like environment ? If not it will not work, you should absolute url in this case

https://mswjs.io/docs/getting-started/integrate/node#direct-usage

I'm using Jest

Could you also provide how do you perform an actual request?

Most of the mismatch cases are due to a typo, wrong method, or relative URL in a NodeJS environment.

There is function for requests which wraps axios.
const res = await netPOST<{key: string}>('auth/login', {email, password});

@olegKusov check the end URL you provide to axios. Looks like you compose it dynamically with one of its pieces resolving to undefined, thus you see a URL like http://undefined/ in the console warning. MSW doesn't create requests, only intercepts them, so the misconfigured URL must be coming from your app's logic.

Otherwise I'd have to see how that abstraction over axios looks like exactly (the axios call) to help more.

One of the contributors has discovered an issue in node-request-interceptor, when we didn't inherit the default hostname in certain scenarios (https://github.com/mswjs/node-request-interceptor/pull/64). This would also cause a URL like http://undefined. Let's keep this in mind until we receive a confirmation from the author on the URL validity on his side.

@olegKusov check the end URL you provide to axios. Looks like you compose it dynamically with one of its pieces resolving to undefined, thus you see a URL like http://undefined/ in the console warning. MSW doesn't create requests, only intercepts them, so the misconfigured URL must be coming from your app's logic.

Otherwise I'd have to see how that abstraction over axios looks like exactly (the axios call) to help more.

I console.log axios.post route => it's auth/login/
image
image

@olegKusov could you please show your axiosConfig ?

@olegKusov could you please show your axiosConfig ?

In this case it's empty object.

I have recreated the issue using a node environment ( no jsdom). The issue will be solved with this PR https://github.com/mswjs/node-request-interceptor/pull/64

The update to node-request-interceptor has been released.

@olegKusov, could you please re-install your dependencies so it gets pulled in and let us know if the issue is gone?

The update to node-request-interceptor has been released.

@olegKusov, could you please re-install your dependencies so it gets pulled in and let us know if the issue is gone?

can you please update msw deps and after that i can update my local deps

@olegKusov could you install the new version of node-request-interceptor by running one of the following command:

You don't need MSW update: it specifies node-request-interceptor dependency version range permissive enough to pull in a patched version release:

https://github.com/mswjs/msw/blob/327b384938b1fdf44e77c5bd79e91b7fab0aaad4/package.json#L73

"^0.5.1" will install "0.5.1" as well as "0.5.3" ("0.5.x")

Neither you should install that 4th party dependency manually. Please reinstall your dependency and the new version of node-request-interceptor will be automatically installed. You can verify that by running the following command after the installation:

$ npm ls node-request-interceptor

You don't need MSW update: it specifies node-request-interceptor dependency version range permissive enough to pull in a patched version release:

https://github.com/mswjs/msw/blob/327b384938b1fdf44e77c5bd79e91b7fab0aaad4/package.json#L73

"^0.5.1" will install "0.5.1" as well as "0.5.3" ("0.5.x")

Neither you should install that 4th party dependency manually. Please reinstall your dependency and the new version of node-request-interceptor will be automatically installed. You can verify that by running the following command after the installation:

$ npm ls node-request-interceptor

It works! Thank you! I close issue.

Glad to hear that! All props to @sean-hernon for an amazing bug fix 馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomalexhughes picture tomalexhughes  路  3Comments

veronesecoms picture veronesecoms  路  3Comments

otaciliolacerda picture otaciliolacerda  路  3Comments

hauptrolle picture hauptrolle  路  4Comments

derekr picture derekr  路  3Comments