supertest icon indicating copy to clipboard operation
supertest copied to clipboard

Error: ECONNREFUSED: Connection refused in Gitlab CI

Open ghost opened this issue 7 years ago • 6 comments

Hi

Running my tests through GItlab CI returns the following error Error: ECONNREFUSED: Connection, but they all pass and work fine locally.

Please see my stackoverflow for more details thread

ghost avatar Jun 29 '18 07:06 ghost

@KayHS I hope the information below still is helpful.

Given the way you are initializing supertest (you are providing an http server instance), it will try to get the port information from the server instance. However the "hostname" information where the server was bound to, is determined by the supertest instance construction and fallbacks to 127.0.0.1 when it is not provided.

See:

  1. Test is created without the host argument: https://github.com/visionmedia/supertest/blob/master/index.js#L25

  2. Given that a server object was provided, the server address is computed: https://github.com/visionmedia/supertest/blob/master/lib/test.js#L36

  3. Since no host was provided, it fallbacks to 127.0.0.1 https://github.com/visionmedia/supertest/blob/master/lib/test.js#L62

And that is what I think is happening. When you are in your local machine your test is probably binding to localhost, however in your CI seems like it is bounding it to a different host, but since supertest is not aware of that it is still trying to reach 127.0.0.1:

Server Running On: runner-sefsf-project-41-concurrent-0gdrs7:3000

The solution in this case, is to either force your test server to be always bound to 127.0.0.1 or to use the request('http://my-domain.com:port') to construct supertest in a way that the server information gets ignored.

jonathansamines avatar Aug 15 '18 04:08 jonathansamines

I have this error too, even after binding using request('http://localhost:5555'); . I still get CONNECTION REFUSED error on travisCI

colibie avatar Dec 22 '18 10:12 colibie

HI @Je-ni a reproduction example would be useful to determine the root of the problem.

jonathansamines avatar Jan 26 '19 03:01 jonathansamines

I also have this error when making multiple parallel requests in a test

ahouck avatar Nov 04 '19 16:11 ahouck

@jonathansamines

The solution in this case, is to either force your test server to be always bound to 127.0.0.1 or to use the request('http://my-domain.com:port') to construct supertest in a way that the server information gets ignored.

I set the address in the test in a test, which works locally like this.

describe('AppController (e2e)', () => {
  let app;

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

  it('/ (GET)', async done => {
    await request('http://localhost:3000') // NOTICE we set the url. Value was: request(app.getHttpServer())
      .get('/')
      .expect(200)
      .expect('Hello World!');
    done();
  });
});

But when I run this in github actions we see this error:

[Nest] 2880   - 10/16/2020, 9:56:53 PM   [ExceptionHandler] Unable to connect to the database. Retrying (2)... +0ms
Error: connect ECONNREFUSED 127.0.0.1:5532
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
FAIL test/app.e2e-spec.ts (10.055s)
  AppController (e2e)
    ✕ / (GET) (5010ms)

...
// much later in the logs we can see
connect ECONNREFUSED 127.0.0.1:3000

kjr247 avatar Oct 16 '20 22:10 kjr247

I also get the same error in my build pipeline, but not only my local machine

OlaoluwaM avatar Jul 19 '21 11:07 OlaoluwaM