Open smallstepstoday opened 6 years ago
@smallstepstoday Hi, I believe that I have the same issue here. Test code:
const axiosMockAdapter = require('axios-mock-adapter')
const axios = require('axios')
const axiosMock = new axiosMockAdapter(axios, {delayResponse: 200})
axiosMock.onGet('/aaa').networkError()
axiosMock.onGet('/bbb').timeout()
axiosMock.onGet('/ccc').reply(500)
test('aaa', done => {
axios.get('/aaa')
.then(() => done())
.catch(e => done())
})
test('bbb', done => {
axios.get('/bbb')
.then(() => done())
.catch(e => done())
})
test('ccc', done => {
axios.get('/ccc')
.then(() => done())
.catch(e => done())
})
test case aaa
and bbb
failed while ccc
passed.
environment:
error log of aaa
:
Error: Network Error at Array.
(D:\Sources\project\acm-statistics\crawler\node_modules\axios-mock-adapter\src\index.js:96:23) at handleRequest (D:\Sources\project\acm-statistics\crawler\node_modules\axios-mock-adapter\src\handle_request.js:50:30) at D:\Sources\project\acm-statistics\crawler\node_modules\axios-mock-adapter\src\index.js:18:9 at new Promise ( ) at MockAdapter. (D:\Sources\project\acm-statistics\crawler\node_modules\axios-mock-adapter\src\index.js:17:14) at dispatchRequest (D:\Sources\project\acm-statistics\crawler\node_modules\axios\lib\core\dispatchRequest.js:59:10) at at process._tickCallback (internal/process/next_tick.js:188:7)
If I delete {delayResponse: 200}
, test case aaa
and bbb
will pass but I'll get a warning like below:
(node:27124) UnhandledPromiseRejectionWarning: Error: Network Error (node:27124) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:27124) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Well, I just tested it on mocha, and all the test passed. While jest fails only when the testEnvironment
is setted to node
.
According to jest's document, the default environment in Jest is a browser-like environment through jsdom. It seems like axios-mock-adapter behaves differently from node environment and 'browser-like' environment. I don't know if it's a bug or feature.
I created a repo to reproduce it.
This is running under jsdom
with react-scripts
1.1.1, and so should not fail.
This could be a duplicate of #116
Could you please try the steps outlined there and confirm? (Note: See this comment)
I have spent the weekend attempting to resolve this issue, and I believe this to be a bug in
axios-mock-adapter
. Having said that, if that is not the case, I hope that someone could point out the flaw in the code below. The documentation on the networkError() function is sparse to say the least, and no amount of stackoverflow.com goodness seems to shed any additional light on this one.My project uses
axios-mock-adapter
at 1.14.1, and I am attempting to simulate a network error (also a timeout, but one thing at a time). I have attempted to cover all the bases in an attempt to trap the Unhandled Promise Rejection that is reported by Node, so the code below is perhaps a bit more verbose than it needs to be. (Happy to receive any pointers...)The code to test is a follows:
The output received when the test runs is:
Thanks for looking into this issue. Has me stumped!