mocha supertest ECONNRESET

2019-03-29 14:40发布

问题:

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?

回答1:

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.