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 has detected the following 3 open handles potentially keeping Jest from exiting
Steps to reproduce:
git clone [email protected]:gandra/node-jest-err-demo.git
cd node-jest-err-demo
npm install
cp .env.example .env
npm run test
npx envinfo --preset jest
output:
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
Any idea how to fix it?
Here is related issue on github: https://github.com/facebook/jest/issues/6446
detectOpenHandles
option is used to detect open handles, it should be normally used. The error warns about potentially open handles:
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');
});
}
For some reason NODE_ENV
is dev
, despite that the documentation states that it's expected to be test
.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With