【问题标题】:Jest did not exit one second after the test run has completed when using supertest and mongoose使用 supertest 和 mongoose 时,Jest 在测试运行完成后一秒没有退出
【发布时间】: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();

    });

【问题讨论】:

    标签: node.js jestjs supertest


    【解决方案1】:

    应用是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.close()
        });
    

    【讨论】:

    • 偶尔这种情况仍在发生,我不确定在这种情况下发生了什么。
    • 是的...我确认它随机卡住了。可笑!
    猜你喜欢
    • 2021-03-01
    • 2021-01-13
    • 2021-12-26
    • 2019-06-15
    • 2023-03-13
    • 2018-12-08
    • 2019-02-05
    • 2019-05-24
    • 2021-03-28
    相关资源
    最近更新 更多