我在尝试使用Supertest在ExpressJ上运行测试时遇到了这些● TCPWRAP或● TCPSERVERWRAP。
Jest检测到以下2个打开的句柄可能会阻止Jest退出:
- TCPWRAP方案
22 |
23 |
> 24 | mongoose.connect(process.env.DB_URL);
| ^
25 |
26 | mongoose.connection.on("connected", () => {
27 | console.log("Connected to MongoDB Successfully");
at makeConnection (node_modules/mongodb/src/cmap/connect.ts:398:18)
at connect (node_modules/mongodb/src/cmap/connect.ts:52:3)
at checkServer (node_modules/mongodb/src/sdam/monitor.ts:305:10)
at node_modules/mongodb/src/sdam/monitor.ts:350:5
at executeAndReschedule (node_modules/mongodb/src/utils.ts:879:5)
at makeInterruptibleAsyncInterval (node_modules/mongodb/src/utils.ts:886:5)
at Monitor.connect (node_modules/mongodb/src/sdam/monitor.ts:153:54)
at Server.connect (node_modules/mongodb/src/sdam/server.ts:241:23)
at createAndConnectServer (node_modules/mongodb/src/sdam/topology.ts:803:10)
at node_modules/mongodb/src/sdam/topology.ts:418:9
at Array.map (<anonymous>)
at Topology.connect (node_modules/mongodb/src/sdam/topology.ts:416:26)
at createTopology (node_modules/mongodb/src/operations/connect.ts:98:12)
at connect (node_modules/mongodb/src/operations/connect.ts:52:10)
at node_modules/mongodb/src/mongo_client.ts:458:14
at maybePromise (node_modules/mongodb/src/utils.ts:462:3)
at MongoClient.connect (node_modules/mongodb/src/mongo_client.ts:457:24)
at node_modules/mongoose/lib/connection.js:809:12
at NativeConnection.Object.<anonymous>.Connection.openUri (node_modules/mongoose/lib/connection.js:798:19)
at node_modules/mongoose/lib/index.js:381:10
at node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at promiseOrCallback (node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose.Object.<anonymous>.Mongoose._promiseOrCallback (node_modules/mongoose/lib/index.js:1234:10)
at Mongoose.Object.<anonymous>.Mongoose.connect (node_modules/mongoose/lib/index.js:380:20)
at Object.connect (index.js:24:10)
at Object.<anonymous> (test/integration/order.route.test.js:1:21)
● TCP服务器包
33 | });
34 |
> 35 | app.listen(PORT, () => {
| ^
36 | console.log("Listening on port, ", PORT);
37 | });
38 |
at Function.listen (node_modules/express/lib/application.js:635:24)
at Object.listen (index.js:35:5)
at Object.<anonymous> (test/integration/order.route.test.js:1:21)
1条答案
按热度按时间8ehkhllq1#
这样做的原因是,当你将应用导入测试并使用它时,它会开始运行你的服务器。因此,你需要分离你的
app.listen()
方法,并将其放在另一个文件中:在运行完每个测试后,您还需要关闭与数据库的连接,否则您将得到● TCPWRAP
。因此,让我们来看看错误实际上来自哪里,以及如何解决它们。
TCPSERVERWRAP
来自测试完成时运行的应用。而TCPWRAP通常来自连接MongoDB(在我的例子中,我使用了mongoose)
若要修正此问题:
请确保将服务器与主应用分离,这样,
server.js
包含:在你的主应用或者
index.js
中,就像我的例子一样;这只会解决
TCPSERVERWRAP
问题。要解决TCPWRAP
问题,您需要将以下内容粘贴到测试文件中:请确保将
process.env.DB_URL
更改为您自己的数据库URL。