Repository navigation
Closed
Labels
Description
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 }
Reacted by Leo Iannacone, Nicolas del Valle, Roberto Oliveira, Nate Wang, Cristian Eriomenco, Mart, roytz, Preco, Alfonso Embid-Desmet, Titus Woo and 19 more
Activity
LeoIannacone commentedon Sep 29, 2016
I got the same error
vrinek commentedon Sep 29, 2016
@itsovereasy could you post how you use nock and how you make the request please?
vrinek commentedon Oct 9, 2016
Thanks for the test examples. I don't see anything obviously wrong with them.
How do you make the actual requests? Which library are you using?
ndelvalle commentedon Oct 24, 2016
Im having a similar problem testing a service using axios. Im testing on phatomjs, does this library also Intercept the browser requests?
ndelvalle commentedon Oct 24, 2016
I see the issue #409 related to
fetch, is the problem related just to that lib or it happens also in other libs likeaxios.paulwithap commentedon Oct 24, 2016
@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.
ndelvalle commentedon Oct 24, 2016
Yeah I would really like to use
nockinstead of stubbing withsinon. But I think I'll do that for now. Thanks @paulwithap.[-]Network Error when using nock[/-][+]Network Error when using nock with axios[/+]vrinek commentedon Oct 28, 2016
Thanks for all the feedback. Next step would be to write some failing tests for axios integration with nock. Would someone like to pick that up?
dragoplut commentedon Nov 23, 2016
My solution by using moxios:
traverse commentedon Dec 9, 2016
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. 👍
supnate commentedon Jan 15, 2017
I finally resolved the issue by this thread: axios/axios#305
Below code from @dyakimenko-p works for me:
ericadamski commentedon Jan 24, 2017
I am having a similar issue. The curious part is that nock works with the first request made to my
/authenticateroute, but fails to intercept the second route.I have fully tried to use moxios as well but I do not have access to the axios instance directly.
Any help or direction would be greatly appreciated.
Here is the output of my test (using Jest)
here is the code:
z-vr commentedon Feb 17, 2017
Just in case it could help someone, I was getting
Network Erroron hosts other thanhttp://localhostbefore I installedisomorphic-fetchfor my react app.aditya1315 commentedon Mar 9, 2017
I have a similar issue while using nock with axios (setup - jest + enzyme, react, redux) when I am testing an async action. When I try to dispatch an action ( store.dispatch(fetchData()).then() ), I get the error
Why is this happening? nock is unable to mock the http request, I have looked at a lot of sources, but could not solve it. Any help is appreciated
code -
stiofand commentedon Mar 16, 2017
This also appears to be happening with prequest requests too.
shai32 commentedon Apr 6, 2017
@aditya1315 the problem is with fetchData, can you print it?
it should return a promise, does it?
also if u use axiox in fetchData, you should also need the adapter (define in the actions file after you import the axios)
axios.defaults.adapter = httpAdapter;
hedgerh commentedon Apr 28, 2017
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.
ronami commentedon Apr 28, 2017
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:
hems commentedon Jul 4, 2018
I believe the ideal solution would be nock to transparently fallback to xhr-mock when appropriate?
as i suggested recently here: #1160 ?
stale commentedon Oct 2, 2018
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.
lock commentedon Oct 23, 2018
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!