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.

MSW error not visible to Axios

See original GitHub issue

Describe the bug

(I’m not sure if this is an issue in axios or in MSW - feel free to redirect me if needed)

When using fetch to do a request to a non-existing MSW resource it throws a helpful MSW error. However this error is not visible when using axios (which just throws a Network error), which can cause a lot of time debugging in the wrong direction.

Environment

  • msw: 0.26.0
  • nodejs: 12.20.1
  • npm: 6.14.10 (but not used)

Please also provide your browser version.

n/a

To Reproduce

Steps to reproduce the behavior:

  1. git clone https://github.com/JCB-K/msw-axios-testcase
  2. cd msw-axios-testcase
  3. yarn
  4. yarn test

Expected behavior

All tests should pass.

Screenshots

image

Axios or MSW?

Like I mentioned above, I’m unsure where the issue lies. Normally I would say axios since that is doing the request and MSW is just the server which should expose a network error. But this is a bit of a different kind of error since it happens in the same environment, and is probably caught in a different way?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
kettanaitocommented, Feb 1, 2021

I see this as yet another reason to tackle #539 as a console.error instead of an exception. For posterity:

  • Exceptions in a request cycle are treated differently depending on a request client (fetch propagates, axios gracefully handlers).
  • Forwarding a suggestion text to stderr results in a suggestion being printed while the original client request error thrown as well (given hitting a non-existing endpoint).
1reaction
JCB-Kcommented, Feb 1, 2021

Nice! I think that https://github.com/mswjs/msw/issues/539 covers my use case. I don’t necessarily need my client-side code to see the error MSW produces (I wouldn’t see it if I requested a “real” server either), I just would want that that error to be visible when running tests since it would be a helpful nudge in the right direction.

When I tested that PR in your repository I got both fetch and axios tests failing. That’s because an unhandled request doesn’t throw an exception, so it never halts the native fetch/axios execution.

What node version are you running? It passes for me on both Node 12 and and Node 14. EDIT: Sorry got it now - you tested the tests in my repo agains your PR. Yes I think it makes sense to have both: a failing request promise on the “client side” (which would also happen in real life), and a console.logged error on the “server” (MSW) side.

Fetch managing to propagate the MSW error is actually the odd one here (not Axios), considering that the server and the client shouldn’t be able to communicate outside regular request<->response.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React MSW axios testing with jest doesnt't get triggered
The reason MSW is not kicking in here is because you are using jest to fully mock axios. So MSW should work if...
Read more >
A Comprehensive Guide to Mock Service Worker (MSW)
Discover our comprehensive guide to Mock Service Worker (MSW) and use the API mocking tool for testing, development, debugging to deliver ...
Read more >
Mocking Lower Layers for Better Test Coverage and Confidence
One option here is to mock the axios module with jest.mock . We probably want to test: ... There are many strategies for...
Read more >
React.js Testing Tutorial #6 - Mock Service Worker - YouTube
In this video we are going to learn how to mock HTTP calls using MSW - mock service worker, while using axios or...
Read more >
Handling Axios errors in Typescript - neldeles
This works great but I encountered some issues with my MSW and Axios setup. Specifically, I was unable to access the error.response.data.message ...
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