【发布时间】:2020-02-13 10:07:21
【问题描述】:
我正在使用 jest 通过 supertest 测试我的 node.js 端点。
但是在运行我的测试 jest 后并没有退出,而是返回以下内容并挂起:
Jest 在测试运行完成后一秒钟没有退出。
这通常意味着存在未执行的异步操作 在您的测试中停止。考虑运行 Jest
--detectOpenHandles解决此问题。
import request from "supertest";
import server from "../../src/index";
import mongoose from "mongoose";
describe("Authentication", () => {
beforeAll( async () => {
console.log("Test Starting...");
await mongoose.connection.dropDatabase();
});
afterAll( async () => {
console.log("... Test Ended");
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
});
it("should authenticate with basic auth", async (done) => {
const BASIC_AUTH = Buffer.from(TEST_VARIABLES.HS_USERNAME + ":" + TEST_VARIABLES.HS_PASSWORD).toString("base64");
const auth_response = await request(server).get("/api/v2/auth/admin")
.set("Authorization", "Basic " + BASIC_AUTH)
.expect(200);
done();
});
【问题讨论】: