如何修复err Jest已检测到以下3个打开的句柄,可能会阻止Jest退出 [英] How to fix err Jest has detected the following 3 open handles potentially keeping Jest from exiting

查看:423
本文介绍了如何修复err Jest已检测到以下3个打开的句柄,可能会阻止Jest退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在使用jest进行测试的某些节点应用程序上开始工作. express-generator用于脚手架.
在第一次测试中,出现以下错误:

Just starting to work on some node app using jest for testing. express-generator used for scaffolding.
On first test I get following error:

Jest已检测到以下3个打开的句柄,有可能阻止Jest退出

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

复制步骤:

git clone git@github.com:gandra/node-jest-err-demo.git   
cd node-jest-err-demo       
npm install   
cp .env.example .env    
npm run test  

npx envinfo --preset jest输出:

npx: installed 1 in 1.896s

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz   Binaries:
    Node: 9.3.0 - /usr/local/bin/node
    Yarn: 1.5.1 - /usr/local/bin/yarn
    npm: 5.7.1 - /usr/local/bin/npm   npmPackages:
    jest: ^23.1.0 => 23.1.0

有什么想法要解决吗?

以下是与github相关的问题: https://github.com/facebook/jest/问题/6446

Here is related issue on github: https://github.com/facebook/jest/issues/6446

推荐答案

detectOpenHandles选项用于检测打开的句柄,通常应使用它.该错误警告可能打开句柄:

detectOpenHandles option is used to detect open handles, it should be normally used. The error warns about potentially open handles:

Jest已检测到以下4个打开的句柄,有可能阻止Jest退出

Jest has detected the following 4 open handles potentially keeping Jest from exiting

即使关闭了句柄,错误仍然会出现.

Even if the handles will be closed, the error will still appear.

此应用程序的实际问题是数据库连接并未真正关闭:

The actual problem with this application is that database connection isn't really closed:

if (process.env.NODE_ENV === 'test') {
  mongoose.connection.close(function () {
    console.log('Mongoose connection disconnected');
  });
}

出于某些原因NODE_ENVdev,尽管

For some reason NODE_ENV is dev, despite that the documentation states that it's expected to be test.

在应用程序启动时立即关闭数据库连接可能会在实际使用数据库连接的单元中引起问题.如指南中所述,MongoDB连接应在测试结束时进行.由于使用了默认的Mongoose连接,因此可以是:

Closing database connection immediately on application start may cause problems in units that actually use database connection. As explained in the guide, MongoDB connection should be at the end of test. Since default Mongoose connection is used, it can be:

afterAll(() => mongoose.disconnect());

这篇关于如何修复err Jest已检测到以下3个打开的句柄,可能会阻止Jest退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆