I have built an api and i want to test some of the endpoints.
I have a number of tests that are similar to the ones below. They are all all failing because of Error: ECONNREFUSED: Connection refused
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",
origin: "localhost:3000",
})
.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",
origin: "localhost:3000",
});
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/p
1) should respond with 200 product_id is authorised
2) should respond with p 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/p-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 p 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/p-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;