“由于拆卸不当而导致泄漏”开玩笑

发布于 2025-01-11 15:05:08 字数 2564 浏览 0 评论 0原文

我正在尝试为使用 Axios 并连接到 mySQL 数据库的 Express API 编写测试。当我在 Jest 中运行测试时,出现以下错误:

A worker process has failed to exit gracefully and has been force exited. This is 
likely caused by tests leaking due to improper teardown. Try running with -- 
detectOpenHandles to find leaks. Active timers can also cause this, ensure that 
.unref() was called on them.

运行 detectorOpenHandles 后,出现以下错误:

Jest has detected the following 3 open handles potentially keeping Jest from exiting:

  ●  TCPWRAP

      15 |
      16 |
    > 17 | const connection = mysql.createConnection(process.env.MYSQL_CONNECTION)
         |                          ^
      18 |
      19 | /**
      20 |  * @swagger

      at new Connection (node_modules/mysql2/lib/connection.js:45:27)
      at Object.createConnection (node_modules/mysql2/index.js:10:10)
      at Object.<anonymous> (src/users/router.js:17:26)
      at Object.<anonymous> (src/index.js:9:25)
      at Object.<anonymous> (src/server.js:1:13)
      at Object.<anonymous> (__tests__/app.test.js:3:13)


  ●  TCPSERVERWRAP

      3 |
      4 |
    > 5 | app.listen(serverPort, () =>
        |     ^
      6 |   console.log(`API Server listening on port ${serverPort}`)
      7 | );
      8 |

      at Function.listen (node_modules/express/lib/application.js:618:24)
      at Object.<anonymous> (src/server.js:5:5)
      at Object.<anonymous> (__tests__/app.test.js:3:13)


  ●  TLSWRAP

      16 | const getMgmtApiJwt = async () => {
      17 |   try {
    > 18 |     const resp = await axios(newRequest);
         |                        ^
      19 |     return resp.data
      20 |   } catch (e) {
      21 |     console.log("did not work");

      at RedirectableRequest.Object.<anonymous>.RedirectableRequest._performRequest (node_modules/follow-redirects/index.js:279:24)
      at new RedirectableRequest (node_modules/follow-redirects/index.js:61:8)
      at Object.request (node_modules/follow-redirects/index.js:487:14)
      at dispatchHttpRequest (node_modules/axios/lib/adapters/http.js:202:25)
      at httpAdapter (node_modules/axios/lib/adapters/http.js:46:10)
      at dispatchRequest (node_modules/axios/lib/core/dispatchRequest.js:53:10)
      at Axios.request (node_modules/axios/lib/core/Axios.js:108:15)
      at axios (node_modules/axios/lib/helpers/bind.js:9:15)
      at getMgmtApiJwt (src/users/controller.js:18:24)
      at Object.<anonymous> (__tests__/app.test.js:182:24)

接下来我可以尝试什么?

I am trying to write tests for an express API that uses Axios and is connected to a mySQL database. I am getting the following error when I run my tests in Jest:

A worker process has failed to exit gracefully and has been force exited. This is 
likely caused by tests leaking due to improper teardown. Try running with -- 
detectOpenHandles to find leaks. Active timers can also cause this, ensure that 
.unref() was called on them.

After running detectOpenHandles, I get the following:

Jest has detected the following 3 open handles potentially keeping Jest from exiting:

  ●  TCPWRAP

      15 |
      16 |
    > 17 | const connection = mysql.createConnection(process.env.MYSQL_CONNECTION)
         |                          ^
      18 |
      19 | /**
      20 |  * @swagger

      at new Connection (node_modules/mysql2/lib/connection.js:45:27)
      at Object.createConnection (node_modules/mysql2/index.js:10:10)
      at Object.<anonymous> (src/users/router.js:17:26)
      at Object.<anonymous> (src/index.js:9:25)
      at Object.<anonymous> (src/server.js:1:13)
      at Object.<anonymous> (__tests__/app.test.js:3:13)


  ●  TCPSERVERWRAP

      3 |
      4 |
    > 5 | app.listen(serverPort, () =>
        |     ^
      6 |   console.log(`API Server listening on port ${serverPort}`)
      7 | );
      8 |

      at Function.listen (node_modules/express/lib/application.js:618:24)
      at Object.<anonymous> (src/server.js:5:5)
      at Object.<anonymous> (__tests__/app.test.js:3:13)


  ●  TLSWRAP

      16 | const getMgmtApiJwt = async () => {
      17 |   try {
    > 18 |     const resp = await axios(newRequest);
         |                        ^
      19 |     return resp.data
      20 |   } catch (e) {
      21 |     console.log("did not work");

      at RedirectableRequest.Object.<anonymous>.RedirectableRequest._performRequest (node_modules/follow-redirects/index.js:279:24)
      at new RedirectableRequest (node_modules/follow-redirects/index.js:61:8)
      at Object.request (node_modules/follow-redirects/index.js:487:14)
      at dispatchHttpRequest (node_modules/axios/lib/adapters/http.js:202:25)
      at httpAdapter (node_modules/axios/lib/adapters/http.js:46:10)
      at dispatchRequest (node_modules/axios/lib/core/dispatchRequest.js:53:10)
      at Axios.request (node_modules/axios/lib/core/Axios.js:108:15)
      at axios (node_modules/axios/lib/helpers/bind.js:9:15)
      at getMgmtApiJwt (src/users/controller.js:18:24)
      at Object.<anonymous> (__tests__/app.test.js:182:24)

What can I try next?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不一样的天空 2025-01-18 15:05:08

实施全局拆卸设置。这应该可以解决这个问题。
在这里看看 Fiehra 的答案: jest 和 mongoose - jest 检测到打开的句柄

Implement a global tear down setup. That should fix this issue.
Take a look at Fiehra's answer here: jest and mongoose - jest has detected opened handles

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文