nock: Network Error when using nock with axios
When I make real calls, my tests are passing fine; however, when I “nock” my requests, they fail with error message “Network Error.” Here is the full error object I’m catching:
{ Error: Network Error
at createError (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/axios/lib/core/createError.js:15:15)
at XMLHttpRequest.handleError [as onerror] (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/axios/lib/adapters/xhr.js:85:14)
at XMLHttpRequest.callback.(anonymous function) (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:251:32)
at invokeEventListeners (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:210:27)
at invokeInlineListeners (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:168:7)
at EventTargetImpl._dispatch (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:126:7)
at EventTargetImpl.dispatchEvent (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:91:17)
at XMLHttpRequest.dispatchEvent (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:71:35)
at dispatchError (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:915:9)
at Request.client.on.err (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:623:11)
at emitOne (events.js:101:20)
at Request.emit (events.js:188:7)
at Request.onRequestError (/Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/request/request.js:813:8)
at emitOne (events.js:96:13)
at OverriddenClientRequest.emit (events.js:188:7)
at /Users/pauljaworski/Projects/OverEasy/over-easy-ui/node_modules/nock/lib/request_overrider.js:206:11
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
config:
{ transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8' },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
method: 'post',
url: '{my url stripped for security}',
data: '{"petitionerId":"abc123","createdAt":"2016-09-26T16:29:28.612Z","updatedAt":null}' },
response: undefined }
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 29
- Comments: 22 (4 by maintainers)
I finally resolved the issue by this thread: https://github.com/mzabriskie/axios/issues/305
Below code from @dyakimenko-p works for me:
My solution by using moxios:
When axios is initializing, it checks if it’s running in browser or node (see here) and it uses the appropriate adapter (http for node, xhr for browser and jsdom).
This means that if you require/import axios after you already have a jsdom globally available, axios will use the xhr adapter and use jsdom’s XMLHttpRequest object.
When axios is getting an error without any details from the browser, it returns a general “Network Error” exception (see here). To get more information about the error you’re getting you can either use jsdom v10.x.x (has a default virtual console that sends everything to node’s console), or if you’re using v9.x.x or bellow, setup a virtual console by yourself:
I’m assuming you’re getting cross-origin errors, if that’s the case, here’s how you can solve it. You can either use axios’s http adapter, which means you don’t go through jsdom’s APIs so no cross origin problems.
For testing react components you probably want to axios to go through jsdom’s APIs, and you want your document running under a domain, and you would like to get cross origin errors if you’re making requests outside of that domain. To make it work, you just need to setup your mocks with nock to return cross origin headers (just like you would on a real browser).
To initialize a jsdom under a domain, just pass a
urloption, like that:Finally, return a cors header from nock mocks:
Is there any progress being made on this? Really would love to use nock instead of any other methods of HTTP mocking.
Edit: I would love to contribute but I’m quite new to testing and don’t really know where to start so if anyone can give me pointers that would be awesome. 👍
I got the same error
@vrinek I’m using axios. That seems to be the issue.
@ndelvalle It seems that nock and axios don’t play well together, and I believe it’s related to the OPTIONS request axios sends before the actual request. I ended up using this method for stubbing my axios requests, and it’s working for me now.
Hopefully nock will work with axios in the future, though, because the syntax is much nicer.
Just in case it could help someone, I was getting
Network Erroron hosts other thanhttp://localhostbefore I installedisomorphic-fetchfor my react app.This problem is related to jsdom, somehow. I switched from using Jest to using Jasmine as my testing framework and axios works out of the box with nock without changing the adapter or anything like that.