Skip to content

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

Closed
@ildella

Description

@ildella

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions