question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Requests not properly resolving with axios in the browser

See original GitHub issue

Describe the bug

One the latest version of axios (0.19.2), and with any version >= 0.18.0 of msw any calls to axios.get do not ever resolve the promise. There are no errors in the console, and MSW and networks logs indicate the request was handled correctly

I haven’t had time to make an isolated reproducible case yet, but will try to do so. In my app the request in question is one where I’m not actually mocking anything, just letting it pass through to the server, but I’m not sure that it’s relevant.

Environment

  • msw: 0.18.0
  • npm: 6.13.4

Please also provide your browser version. Replicated in Firefox 77.0.1 and Chrome 83.0.4103.61

To Reproduce

Steps to reproduce the behavior:

Should be reproducible with axios code like

axios.get("/foo").then(res=> console.log(res))

Expected behavior

Should log the response out to the console, but instead we never seem to make it inside the then statement. A clear and concise description of what you expected to happen.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewSwerlickcommented, Jun 15, 2020

@kettanaito yeah everything is working fine. Basically I had one file that had my handler definitions, my worker setup and my server setup. I broke it into 3 seperate files, and ensured that I only imported the one in the bundle used in the browser.

1reaction
AndrewSwerlickcommented, Jun 15, 2020

Got a reproduction! https://github.com/AndrewSwerlick/examples.

The gist of it is that it appears if you ever call setupServer from msw/node in the browser it borks axios. I was trying to consolidate and organize my code by having a single module that exported both a nodeSever and a browserServer and then I imported and started the right one depending on the environment I was running in (jest vs browser). I assumed setupServer was side effect free, and nothing would happen until we called listen, but looking at the code it appears that’s not the case.

It may be worth modifying the setupServer function to include a warning if running in a browser environment, or something like that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios POST request not working - Stack Overflow
When I try to send a POST request with axios from ReactJS, I get a 405 Method Not Allowed error on the OPTIONS...
Read more >
How to make HTTP requests with Axios - LogRocket Blog
Axios is a client HTTP API based on the XMLHttpRequest interface provided by browsers. In this tutorial, we'll demonstrate how to make HTTP ......
Read more >
How To Use Axios With React: The Definitive Guide (2021)
In this guide, you will see exactly how to use Axios.js with React using tons of real-world examples featuring React hooks.
Read more >
Axios vs. Fetch API – Which is Better For HTTP Requests?
Both Axios and fetch() can be used within a browser, as well as a ... For example, if we're not authenticated to receive...
Read more >
Handling Errors | Axios Docs
axios.get('/user/12345') .catch(function (error) { if (error.response) { // The ... `error.request` is an instance of XMLHttpRequest in the browser and an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found