最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用supertest和猫鼬在测试运行完成后一秒钟没有退出

运维笔记admin98浏览0评论

使用supertest和猫鼬在测试运行完成后一秒钟没有退出

使用supertest和猫鼬在测试运行完成后一秒钟没有退出

我使用玩笑来通过supertest测试我的node.js端点。

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

测试运行一秒钟后,开玩笑没有退出。

这通常意味着有些异步操作不是在测试中停止了。考虑使用--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服务器仍在侦听。

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()
    });
发布评论

评论列表(0)

  1. 暂无评论