【发布时间】:2021-01-12 17:51:02
【问题描述】:
我正在尝试使用 mocha 和 chai 测试我的 express 服务器,但测试完成后我无法关闭服务器连接。
索引.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;
我想知道如何在测试执行后关闭服务器。目前正在测试,但测试后它挂起。
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();
});
});
});
});
【问题讨论】:
标签: javascript node.js express mocha.js chai