gpt4 book ai didi

node.js - mongoose.connect 在开 Jest 测试后留下打开的句柄

转载 作者:行者123 更新时间:2023-12-05 01:28:17 25 4
gpt4 key购买 nike

如何去掉 jest 测试中 mongoose.connect 留下的 openHandles

const { startTestDB } = require("../conn");
const mongoose = require("mongoose");
const request = require("supertest");
const faker = require("faker");
const app = require("../app");
const fs = require("fs");

describe("test1: ", () => {
beforeAll(() => {
startTestDB()
.then(() => {
console.log("Connected to db.");
})
.catch((e) => {
console.log(e);
});
});

afterAll(() => {
mongoose
.disconnect()
.then(() => {
console.log('db connection is closed');
})
.catch((e) => {
console.error(e);
});
});

it("Should save a new user", async () =>
request(app)
.post("/save")
.send({
name: faker.internet.userName(),
email: faker.internet.email()
})
.expect(201));
});

这是一个具有相同问题的示例代码,当我运行 npx jest --detectOpenHandles 时,我在 mongoose.connect 找到了一个,据我所知打开句柄由于在测试之前未完成的异步操作而发生,但我在 afterAll 中明确关闭了连接。

我得到的错误信息:

  ●  TLSWRAP

9 |
10 | module.exports.startTestDB = () =>
> 11 | mongoose.connect(
| ^
12 | `mongodb+srv://${mongodb.user}:${mongodb.password}@${mongodb.host}/${mongodb.db}-test?retryWrites=true&w=majority`,
13 | {
14 | useNewUrlParser: true,

at parseSrvConnectionString (node_modules/mongodb/lib/core/uri_parser.js:67:7)
at parseConnectionString (node_modules/mongodb/lib/core/uri_parser.js:597:12)
at connect (node_modules/mongodb/lib/operations/connect.js:282:3)
at node_modules/mongodb/lib/mongo_client.js:260:5
at maybePromise (node_modules/mongodb/lib/utils.js:692:3)
at MongoClient.Object.<anonymous>.MongoClient.connect (node_modules/mongodb/lib/mongo_client.js:256:10)
at node_modules/mongoose/lib/connection.js:835:12
at NativeConnection.Object.<anonymous>.Connection.openUri (node_modules/mongoose/lib/connection.js:832:19)
at node_modules/mongoose/lib/index.js:351:10
at node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at promiseOrCallback (node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose.Object.<anonymous>.Mongoose._promiseOrCallback (node_modules/mongoose/lib/index.js:1149:10)
at Mongoose.connect (node_modules/mongoose/lib/index.js:350:20)
at startTestDB (conn.js:11:12)
at tests/s.test.js:10:5
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:387:19)
at _run10000 (node_modules/@jest/core/build/cli/index.js:408:7)
at runCLI (node_modules/@jest/core/build/cli/index.js:261:3)

最佳答案

我正在重新发布这个问题的答案 jest and mongoose - jest has detected opened handles因为这是我解决问题的方法。

像许多其他人建议的那样关闭连接对我不起作用。

在搜索有关 SO 和 github 问题的解决方案数小时后,我遇到了这个 github 线程 https://github.com/visionmedia/supertest/issues/520提供多种解决方案。最终对我有用的是在我的根目录中实现一个全局拆卸文件,如下所示:

// test-teardown-globals.js
module.exports = () => {
process.exit(0);
};

还通过稍微调整我的 jest.config.js

// jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globalTeardown: '<rootDir>/test-teardown-globals.js',
};

更新:虽然这可行,但此解决方案仅适用于在本地开发和运行测试。我也更喜欢 Özgür Atmaca 的解决方案,在 beforeAll block 中重新连接之前断开连接

关于node.js - mongoose.connect 在开 Jest 测试后留下打开的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68826970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com