Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest and Nock return Network Error when making requests with Axios #938

Closed
ildella opened this issue Jun 5, 2017 · 3 comments
Closed

Jest and Nock return Network Error when making requests with Axios #938

ildella opened this issue Jun 5, 2017 · 3 comments

Comments

@ildella
Copy link

ildella commented Jun 5, 2017

Summary

Testing async code with Jest or Nock always return Network Error.

This is reported by others:
https://www.bountysource.com/issues/38003315-network-error-when-using-nock-with-axios
https://stackoverflow.com/questions/42677387/jest-returns-network-error-when-doing-an-authenticated-request-with-axios

I've actually used the first solution suggested on that StackOverflow: "Solution change axios adapter to http". That actually works.

Without forcing the http adapter, running "npm test" with Jest cause the test to never complete and stay stuck forever.

I am not sure this is an Axios issue rather than Jest or Nock and this is not the point.

What is strange is the stacktrace below: it seem to me that is running the xhr implementation. Isn't this strange in the node environment?
Is there a simpler way to force using the http adapter than the one advised on StackOverflow?

Network Error

      
      at createError (node_modules/axios/lib/core/createError.js:16:15)
      at XMLHttpRequest.handleError [as onerror] (node_modules/axios/lib/adapters/xhr.js:87:14)
      at XMLHttpRequest.callback.(anonymous function) (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:289:32)
      at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:219:27)
      at invokeInlineListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:166:7)
      at EventTargetImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:122:7)
      at EventTargetImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
      at XMLHttpRequest.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:61:35)
      at dispatchError (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:994:9)
      at validCORSHeaders (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:1009:7)
      at receiveResponse (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:871:12)
      at EventEmitter.client.on.res (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:691:38)
      at emitOne (events.js:96:13)
      at EventEmitter.emit (events.js:191:7)
      at Request.realClient.on.res (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:281:49)
      at emitOne (events.js:96:13)
      at Request.emit (events.js:191:7)
      at Request.onRequestResponse (node_modules/request/request.js:1074:10)
      at emitOne (events.js:101:20)
      at ClientRequest.emit (events.js:191:7)
      at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
      at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
      at Socket.socketOnData (_http_client.js:411:20)
      at emitOne (events.js:96:13)
      at Socket.emit (events.js:191:7)
      at readableAddChunk (_stream_readable.js:178:18)
      at Socket.Readable.push (_stream_readable.js:136:10)
      at TCP.onread (net.js:561:20)

Context

  • axios version: v0.16.2
  • Environment: node v7.10, Ubuntu 17.04 Linux 4.10
@rubennorte
Copy link
Member

If you take a look at your stack trace you'll notice there are traces of jsdom. That means you're using a jsdom/browser environment rather than node. It's just a jest configuration issue.

@iggirex
Copy link

iggirex commented Apr 13, 2018

I changed the test script in package.json to "test": "react-scripts test --env=node" but it's still not working for me. Is there another place where this needs to be changed? (My project used create-react-app)

@cwtuan
Copy link

cwtuan commented Jan 10, 2019

jest --env=node works for me.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants