14

I'm testing a Nodejs server with Mocha and Supertest. The test suite has grown to more than 1500 tests. Suddenly, although all of the code under test still works, my test suite fails with this error:

{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

If I comment out some tests that run earlier, the tests that cause the error change. What is causing this insanity?

Christian Smith
  • 8,089
  • 3
  • 19
  • 18

1 Answers1

11

I found the answer in this Google Groups post by Mike Gradek:

We used mocha and supertest to issue these requests, and realized that we were actually spinning up new port bindings on every request instead of reusing existing bindings.

So code that was written like so:

var request = require('supertest');
var app = require('../app');
request(app).get(...);
request(app).get(...);

Became

var request = require('supertest');
var app = require('../app');
var supertest = request(app);
supertest.get(...);
supertest.get(...);

That solved the issue for us.

And for me as well.

Community
  • 1
  • 1
Christian Smith
  • 8,089
  • 3
  • 19
  • 18
  • 1
    Okay, but *why* did it solve the issue? Using up all the ephemeral ports? I'm getting the same issue with even the 2nd version when I get enough tests... – Paul S Nov 08 '17 at 00:25
  • 1
    I am getting the same problem even though I created "supertest" only once if I do enough parallel http requests – Felipe Taiarol Jun 22 '19 at 13:32
  • 1
    Like Felipe Taiarol, I also get the same problem even creating supertest only once if there are sufficient parallel http requests. My solution was to serialize all the http requests, though this slows down the suite. Specifically, with bluebird promises, Promise.map(..., ..., { concurrency: 1 }) resolved the issue for me. – Tim Heilman Jun 16 '21 at 18:45
  • Was able to fix our issue by switching from `Promise.all` with three supertest requests to running them one after the other with individual `await`. – Jesse Carter Jun 29 '21 at 18:02
  • @PaulS from the Google groups post: "we ended up using many more file descriptors than we'd anticipate due to our test setup, and the OS started killing them causing the RST to get sent. We fixed up our tests and haven't seen the RST since." – Herman J. Radtke III Jul 27 '21 at 16:22