首页 > 解决方案 > 升级到最新版本时的开玩笑错误 - 工作进程未能正常退出

问题描述

我最近将我的 jest 包更新到 v24 的最新版本,并且在运行我的单元测试时,我在测试套件运行结束时得到了这个:

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 --runInBand --detectOpenHandles to find leaks.

我添加--detectOpenHandles了我的测试命令,消息在下一次运行中发送。我不知道如何看待它,因为 detectOpenHandles 旨在查找打开的句柄而不是修复它,所以消息如何不会在下一次运行中出现,并且没有其他消息可以指导我解决可能的问题。

有没有人遇到过类似的行为?补充一下,我现在使用的是最新版本的 jest,即 27.2.0。

标签: reactjsjestjs

解决方案


根据我的研究,错误发生在以下情况:

  • 异步函数未完成
  • 承诺功能未完成
  • Websocket、数据库或任何其他具有连接/断开方法的东西在测试结束前没有断开连接

该错误没有提到即使使用 --detectOpenHandles 标志,测试也会在大多数情况下挂起,这可能是您没有看到任何有用的调试消息的原因。

从文档中:

Attempt to collect and print open handles preventing Jest from exiting cleanly. Use this in cases where you need to use --forceExit in order for Jest to exit to potentially track down the reason. This implies --runInBand, making tests run serially. Implemented using async_hooks, so it only works in Node 8 and newer. This option has a significant performance penalty and should only be used for debugging.

正如文档中所说,您可以尝试使用 --detectOpenHandles 和 --forceExit 标志运行测试。这意味着测试将连续运行,并且当测试由于不正确的拆卸而无法完成时,每个测试都会强制退出。让我知道这个是否奏效。


推荐阅读