使用supertest和mongoose进行测试运行后,Jest没有退出一秒钟

时间:2019-10-16 15:07:12

标签: node.js jestjs supertest

我正在开玩笑地用supertest测试我的node.js端点。

但是,在运行我的测试玩笑之后,它不会退出,而是返回以下内容并挂起:

  

测试运行一秒钟后,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();

    });

1 个答案:

答案 0 :(得分:0)

该应用是node.js服务器仍在侦听。

server = app.listen(this.config.port, "0.0.0.0");

将server.close()添加到afterAll解决了该问题。

  afterAll( async () => {
        console.log("... Test Ended");
        await mongoose.connection.dropDatabase();
        await mongoose.connection.close();
        app.cose()
    });