Supertest: Error: ECONNREFUSED: Connection refused in Gitlab CI

Created on 29 Jun 2018  Â·  5Comments  Â·  Source: visionmedia/supertest

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

All 5 comments

@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
  1. Given that a server object was provided, the server address is computed: https://github.com/visionmedia/supertest/blob/master/lib/test.js#L36

  2. 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.

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

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

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

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nitrocode picture nitrocode  Â·  4Comments

schm1ty1 picture schm1ty1  Â·  4Comments

nazreen picture nazreen  Â·  3Comments

mayacr86 picture mayacr86  Â·  4Comments

UniverseHan picture UniverseHan  Â·  4Comments