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.

make mocking network errors work for intercepted axios requests

See original GitHub issue

Is your feature request related to a problem? Please describe. I am currently building a httpClient for a browser-based app. I am using msw (it is awesome!) to cover edge-cases like network failures with tests on network level. The client basically wraps axios. I was looking forward to response.networkError() that was added in https://github.com/mswjs/msw/pull/253 because I needed a way to simulate a network problem (like being offline).

I am using networkError(someCustomMessage) in the response resolver. I am asserting that the error caught by axios states my custom message. Instead I am seeing the following error

{
  "message": "Request aborted",
  "code": "ECONNABORTED"
  "stack": "Error: Request aborted
    at createError (D:\dev\kraken\core\node_modules\axios\lib\core\createError.js:16:15)
    at XMLHttpRequestOverride.handleAbort [as onabort] (D:\dev\kraken\core\node_modules\axios\lib\adapters\xhr.js:73:14)
    at XMLHttpRequestOverride.trigger (D:\dev\kraken\core\node_modules\node-request-interceptor\lib\XMLHttpRequest\XMLHttpRequest\createXMLHttpRequestOverride.js:100:92)
    at XMLHttpRequestOverride.abort (D:\dev\kraken\core\node_modules\node-request-interceptor\lib\XMLHttpRequest\XMLHttpRequest\createXMLHttpRequestOverride.js:252:22)
    at D:\dev\kraken\core\node_modules\node-request-interceptor\lib\XMLHttpRequest\XMLHttpRequest\createXMLHttpRequestOverride.js:182:27";
  "//": "..more stuff I stripped from the error"
} 

My error handling logic goes on and treats the error as timeout error, which was not my intention.

Describe the solution you’d like I would like to augment the simulation of network errors in a way that axios treats the error as network error (error.message === 'Network Error', see https://github.com/axios/axios/issues/383).

I would therefore like to add respective tests to https://github.com/mswjs/msw/tree/master/test/msw-api/res.

Additional context

  1. I can create a minimal reproduction repo if that helps.
  2. I would very much like to contribute here if that is possible.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
s-piccommented, Sep 10, 2020

Yip, that fixed it 😅 Thanks.

1reaction
kettanaitocommented, Sep 6, 2020

The fix is published in node-request-interceptor@0.5.1. MSW update to be issued.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Intercept requests with nock · Issue #305 · axios/axios - GitHub
Hi, i wanted to know if it is posible to intercept requests made by axios ... Then I get Network Error on the...
Read more >
Mock axios using axios interceptors - Stack Overflow
Module that I want to test * Intercepts every axios request and redirects to login on 401 */ import axios from 'axios'; export...
Read more >
Don't Mock Fetch (or Axios) - YouTube
Despite having launched a video showing how to mock both Fetch and Axios recently... here is another solution, the one recommended by Kent ......
Read more >
Mock Service Worker adopts a brand-new request interception ...
Conventional API mocking solutions turn any fetch or axios call into a black hole through the substitution of the entire client with a...
Read more >
How to Emulate Axios/Fetch to Handle HTTP Requests in ...
msw stands for Mock Service Worker , a tool that takes care of intercepting all requests made at your network level.
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