mongoose 如何使用Express和Supertest解决● TCPWRAP或● TCPSERVERWRAP问题

w8biq8rn  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(148)

我在尝试使用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)
8ehkhllq

8ehkhllq1#

这样做的原因是,当你将应用导入测试并使用它时,它会开始运行你的服务器。因此,你需要分离你的app.listen()方法,并将其放在另一个文件中:在运行完每个测试后,您还需要关闭与数据库的连接,否则您将得到● TCPWRAP
因此,让我们来看看错误实际上来自哪里,以及如何解决它们。
TCPSERVERWRAP来自测试完成时运行的应用。

> 35 | app.listen(PORT, () => {
     |     ^
  36 |   console.log("Listening on port, ", PORT);
  37 | });

而TCPWRAP通常来自连接MongoDB(在我的例子中,我使用了mongoose)

> 24 | mongoose.connect(process.env.DB_URL);

若要修正此问题:

请确保将服务器与主应用分离,这样,server.js包含:

// server.js
const app = require('./index');

const PORT = 3334;

app.listen(PORT, () => {
  console.log("Listening on port, ", PORT);
});

在你的主应用或者index.js中,就像我的例子一样;

//app.js or index.js
const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.status(200).send("Hello World!");
});

module.exports = app;

这只会解决TCPSERVERWRAP问题。要解决TCPWRAP问题,您需要将以下内容粘贴到测试文件中:

const mongoose = require('mongoose');

beforeEach(async () => {
  await mongoose.connect(process.env.DB_URL);
})

afterEach(async () => {
  await mongoose.connection.close();
});

请确保将process.env.DB_URL更改为您自己的数据库URL。

相关问题