在nodeJS中使用Jest运行这个集成测试时:
const request = require("supertest");
let server;
describe("/api/chat", () => {
beforeEach(() => {
server = require("../../api");
});
describe("GET /userlist", () => {
it("show userlist", async () => {
const result = await request(server)
.get("/api/chat/userlist")
.set("X-Auth", process.env.XAuth);
expect(result.status).toBe(200);
});
});
afterAll(done => {
server.close(done);
});
});
使用api.js文件:
const PORT = process.env.PORT;
const server = app.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});
app.use("/api/chat", chat);
module.exports = server;
我遇到一个错误,有些东西阻止它退出:
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● TCPSERVERWRAP
21 | const PORT = process.env.PORT;
22 |
> 23 | const server = app.listen(PORT, () => {
| ^
24 | console.log(`listening on port ${PORT}`);
25 | });
26 |
有什么想法吗?我已经在Github上检查过了,但没有什么真正有用的,或者只是一种变通方法。如何正确地关闭连接?
--forceExit
或--maxWorkers=10
参数了吗? - Ndx