chai-http icon indicating copy to clipboard operation
chai-http copied to clipboard

Occurred 'read ECONNRESET' error when server is listening port only 3000

Open hyeoksuhan opened this issue 6 years ago • 1 comments

It is not clear and some weird. read ECONNRESET exception is occurred when I use chai.request() with server that is listening port 3000 but the others port.

Problem code is here: ./test/test.js

const chai = require('chai');
chai.use(require('chai-http'));

const Koa = require('koa');
const app = new Koa();

koa.use(ctx => {
  ctx.body = 'hello';
});

const server = koa.listen(3000); // problem

describe('Test', () => {
  it('should get hello', () => {
    chai.request(server)
    .get('/')
    .end((err, res) => {
      if (err) {
        console.error(err);
      }
    });
});

package.json

{
  "scripts": {
    "test": "mocha --recursive"
  },
  "dependencies": {
    "chai": "^4.1.2",
    "chai-http": "^3.0.0",
    "mocha": "^5.0.2",
  },
}

run

$ yarn test

result

read ECONNRESET
  at exports._errnoException (util.js:1022:11)
  at TCP.onread (net.js:610:25 

node: v8.9.4 yarn: v1.3.2

If I change the port from 3000 to the other port such as 3001, 1313 then the error is not occurred. Why does it happen? My environment has some problem or port 3000 is reserved in chai or the others?

I tried to catch exceptions but there's no errors.

process.on('uncaughtException', function (err) {
  console.log(err);
})
const server = koa.listen(3000).on('error', err => {
  console.log(err);
});

hyeoksuhan avatar Mar 07 '18 04:03 hyeoksuhan

Port 3000 is definitely not reserved in chai-http, nor should it be on your local computer. I haven't worked with koa in a long time - is starting the server synchronous? Could it be possible that there is a race condition where the server might still be starting when the test is run?

austince avatar May 27 '19 18:05 austince