I'm trying to test my express server using mocha and chai but i'm not able to close the server connection once the test has been completed.
Index.js
const express = require('express');
const dbconnection = require('./dbConnection.js');
const app = express();
.....
(async ()=>{
await dbconnection.init();
/* Loading middleware and stuff */
const server = app.listen(port, host, ()=>{
console.log('Server Started!')
app.emit('ready');
});
})()
module.exports = app;
I would like to know how to close the server once the test is executed. Currently testing is working but after the test it hangs.
server.test.js
const server = require("../../index");
const chai = require("chai");
const chaiHttp = require("chai-http");
const should = chai.should();
chai.use(chaiHttp);
before(function (done) {
this.timeout(15000);
server.on("ready", () => {
done();
});
});
describe.only("Health Check Test", function () {
describe("/GET healthy", () => {
it("it should GET the health status", (done) => {
chai
.request(server)
.get("/healthy")
.end((error, res) => {
res.should.have.status(200);
done();
});
});
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…