Jest: Async example: Error: connect ECONNREFUSED 127.0.0.1:80

Created on 17 Oct 2016  Â·  15Comments  Â·  Source: facebook/jest

HI , I'm on the window 7, when i run npm test in examples/async, it produced errors below:

$ npm test

> @ test D:\study\jest\examples\async
> jest


 RUNS  __tests__\user-test.js
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:80
    at Object.exports._errnoException (util.js:1026:11)
    at exports._exceptionWithHostPort (util.js:1049:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1085:14)
npm ERR! Test failed.  See above for more details.

then, I checked the 80 port, but found nothing:

Administrator@USER-20150722FJ D:\study\jest\examples\async      
$ netstat -ano | findstr "127.0.0.1:80"                         

Help Wanted

Most helpful comment

@dYb Did you run Jest with --no-cache once, or ran it for the first time with babel-jest > '9.0.3'? Which version of Node are you running?

I have this issue with all ^9.0.0 versions of babel-jest ( '9.0.1', '9.0.2', '9.0.3'). '10.0.1' works and so does 16 and 17.

edit: tested here on a Windows 10 machine, babel-jest ^9.0.0 not working, 16 and 17 works fine.

All 15 comments

Same problem here. Looks like the request is actually fired and the mock is not used.

Changing the host is reflected in the error message:

export default function request(url) {
  return new Promise(resolve => {
    // This is an example of an http request, for example to fetch
    // user data from an API.
    // This module is being mocked in __mocks__/request.js
    http.get({
      host: '--TESTHOST--',
      path: url}, response => {
      let data = '';
      response.on('data', _data => data += _data);
      response.on('end', () => resolve(data));
    });
  });
}
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND --TESTHOST-- --TESTHOST--:80
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

Any reason why this is happening?

Tried mocking my app and it was bypassing the mock. Pulled the examples and also is being bypassed.

Use require instead of import. In user-test.js, change:

import * as user from '../user';

to

var user = require('../user')

NodeJS has no support for import (yet).

I'm unsure what the problem here is. Maybe on windows manual mocking doesn't work as well as it should? :(

@frevib with babel-jest and the es-modules transform, imports will be compiled to CommonJS, so that isn't the issue here.

@cpojer You're right, this isn't the issue, although it works somehow for me (I'm on OSX Sierra).

The example uses babel-jest ^9.0.0. Upgrading to 16.0.0 and running jest --no-cache once also solved the issue for me.

@dYb is this still an issue with Jest 17?

@thymikee still an issue with 17. same error stack

@dYb Did you run Jest with --no-cache once, or ran it for the first time with babel-jest > '9.0.3'? Which version of Node are you running?

I have this issue with all ^9.0.0 versions of babel-jest ( '9.0.1', '9.0.2', '9.0.3'). '10.0.1' works and so does 16 and 17.

edit: tested here on a Windows 10 machine, babel-jest ^9.0.0 not working, 16 and 17 works fine.

@frevib
Thank you for your help, it works, I just updated babel-jest version to ^17.0.2.

@dYb as a rule of thumb, you should always keep Jest's helper packages (like babel-jest) in sync to the major version to avoid such problems.

@thymikee but its version is assigned in package.json, I think it should be updated. The error above was happened when I run the async example of this repository

Thank you for your time. ^_^

There's no way for one package (e.g. "jest": "17.0.3") to update another one in your package.json. You need to do it manually (or use some kind of CLI which will do it for you, but unfortunately we currently don't have anything like that for Jest).

It's very similar case for updating react to the next version – you also bump version of react-dom and, let's say, react-addons-test-utils to the same major version, to avoid inconsistencies between two incompatible versions.

@dYb it definitely has to be updated! @frevib is already working on that.
Thanks for catching that up :)

Yep, on Windows 7 machine had the same issue ,Was unable to mock and the actual call was happening instead of mocking, Upgrading babel-jest from 9.0 to 17.0.2 and it works perfectly .

Was this page helpful?
0 / 5 - 0 ratings