8

Nodejs和Jest单元测试无法正常退出的问题

 2 years ago
source link: https://segmentfault.com/a/1190000040847306
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nodejs和Jest单元测试无法正常退出的问题

发布于 16 分钟前

最近在编写Nodejs/Jest单元测试时出现应用无法正常退出,明明单元测试已经执行完毕,但是应用就是卡住,无法正常退出,导致单元测试无法连续执行。由于应用较为复杂,很难以排查是什么原因导致的,经过分析原因主要如下:

  1. 未处理async/Promise会导致该原因,解决方法是在jest中添加--detectOpenHandles参数,添加该参数后,在运行时jest会检测并给出提示,依此解决就可以。
  2. 还有一种情况是应用中存在未清理的setInterval也会导致此情况,这种情况比较隐蔽。解决方法也很简单,可以自己编写一个setSafeInterval函数,替换一个应用中所有使用setInterval的地方,记录一下定时器句柄,在退出前清理一下就可以了。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK