CI/CD: Error: ECONNREFUSED: Connection refused
I have a nodejs, express api built using typescript and a mongo database.
I am using mocha, chai and supertest to write my tests. Although they work locally they are all failing when they go through the test job. The pipeline fails on the test stage. Giving the following error: ECONNREFUSED: Connection refused.
gitlab-ci.yml
stages:
- quality
- test
- build
- deploy
image: node:8.11.3
services:
- mongo
before_script:
- npm install
quality:
stage: quality
script:
- npm run-script lint
test:
stage: test
script:
- npm run-script test
build:
stage: build
script:
- npm run-script build
deploy:
stage: deploy
script:
- echo "TODO deploy"
tests
import { assert, expect } from "chai";
import request from "supertest";
import app from "./../src/index";
describe("Authentication", () => {
it("should respond with 200 product_id is authorised", async () => {
const result = await request(app).post("/api/auth")
.send({
product_id: "123",
})
.expect("Content-Type", /json/)
.expect(200);
});
it("should respond with session token", async () => {
const result = await request(app).post("/api/auth")
.send({
product_id: "123",
});
expect(result.body.data).to.have.property("token");
});
});
package.json
"test": "mocha -r ts-node/register --project tsconfig.json test/*.test.ts --exit"
errors:
> mocha -r ts-node/register --project tsconfig.json test/*.test.ts --exit
Server Running On: runner-sefsf-project-41-concurrent-0gdrs7:3000
Authentication
MongoDB Successfully Connected On: mongodb://localhost:27017/paycasso
1) should respond with 200 product_id is authorised
2) should respond with session token
Server
3) should be up
4) should throw 404 for unrecognized routes
Transaction
5) should respond with a new transction
0 passing (40ms)
5 failing
1) Authentication
should respond with 200 product_id is authorised:
Error: ECONNREFUSED: Connection refused
at Test.assert (node_modules/supertest/lib/test.js:165:15)
at assert (node_modules/supertest/lib/test.js:131:12)
at /eng/paycasso-server/node_modules/supertest/lib/test.js:128:5
at Test.Request.callback (node_modules/superagent/lib/node/index.js:718:3)
at ClientRequest.req.once.err (node_modules/superagent/lib/node/index.js:646:10)
at Socket.socketErrorListener (_http_client.js:382:9)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
2) Authentication
should respond with paycasso session token:
Error: ECONNREFUSED: Connection refused
at Test.assert (node_modules/supertest/lib/test.js:165:15)
at assert (node_modules/supertest/lib/test.js:131:12)
at /eng/paycasso-server/node_modules/supertest/lib/test.js:128:5
at Test.Request.callback (node_modules/superagent/lib/node/index.js:718:3)
at ClientRequest.req.once.err (node_modules/superagent/lib/node/index.js:646:10)
at Socket.socketErrorListener (_http_client.js:382:9)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
Apparently the connection is being refused? But i'm not sure what it means by this specifically because you can see the server in the error log is connected and so is the mongo service.
index.ts
import Server from "./server";
export default new Server().server;
server.ts
import App from "./app";
class Server {
public server: any;
private instance: any;
private app: any;
private config: any;
constructor() {
this.instance = new App();
this.app = this.instance.app;
this.config = this.instance.config;
this.server = this.app.listen(this.config.port, this.config.hostname);
console.log("Server Running On: " + this.config.hostname + ":" + this.config.port);
}
}
export default Server;
Edited by Kay Khan