TLSWRAP open handle wrongly detected with MongoDB connections
See original GitHub issue💥 Regression Report
Running Jest with --detectOpenHandles detects a TLSWRAP potential open handle on MongoDb connections, even if they are properly closed in an “afterAll” enclosure and jest exits without issues. The exact message is:
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● TLSWRAP
6 |
7 | beforeAll(async () => {
> 8 | await client.connect()
| ^
9 | })
10 |
11 | afterAll(async () => {
at Object.resolveSRVRecord (node_modules/mongodb/src/connection_string.ts:69:7)
at Object.connect (node_modules/mongodb/src/operations/connect.ts:51:12)
at node_modules/mongodb/src/mongo_client.ts:410:7
at Object.maybePromise (node_modules/mongodb/src/utils.ts:618:3)
at MongoClient.connect (node_modules/mongodb/src/mongo_client.ts:409:12)
at app.test.js:8:16
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:387:19)
at _run10000 (node_modules/@jest/core/build/cli/index.js:408:7)
at runCLI (node_modules/@jest/core/build/cli/index.js:261:3)
This seems to happen with MongoDb’s official NodeJs driver, and, by extension, any other packages that use that driver (in my case I got it with mongoose and connect-mongo-session)
Last working version
Worked up to version: 26.6.3
Stopped working in version: 27.0.0
To Reproduce
NOTE: A valid MongoDB URI is needed to reproduce this issue.
Within an empty directory:
- Create a new empty Node project with:
npm init
- Install jest and mongodb
npm install jest mongodb
- Create a app.test.js file with following contents (add a valid MongoDB URI where necessary):
const { MongoClient } = require("mongodb");
// Replace the uri string with your MongoDB connection string.
const uri ='YOUR-MONGODB-URI'
const client = new MongoClient(uri)
beforeAll(async () => {
await client.connect()
})
afterAll(async () => {
await client.close()
})
test('testing', () => {
expect(client).toBeDefined()
})
- Run the test with jest and --detectOpenHandles
jest --detectOpenHandles
Expected behavior
No open handles should be found.
Link to repl or repo (highly encouraged)
Use the code snippet provided above.
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS 11.4
CPU: (8) arm64 Apple M1
Binaries:
Node: 16.4.2 - /opt/local/bin/node
npm: 7.19.1 - /opt/local/bin/npm
npmPackages:
jest: ^27.0.0 => 27.0.6
Thank you for your help! ❤️
Issue Analytics
- State:
- Created 2 years ago
- Reactions:19
- Comments:24 (2 by maintainers)
Top Results From Across the Web
mongoose.connect leaves open handles after jest test
I am reposting my answer from this question jest and mongoose - jest has detected opened handles as this was my solution for...
Read more >Troubleshoot Connection Issues — MongoDB Atlas
Close any open connections to your database deployment not currently in use. Scale your cluster to a higher tier to support more concurrent...
Read more >Using with MongoDB - Jest
Jest MongoDB provides all required configuration to run your tests using MongoDB. ... connection = await MongoClient.connect(globalThis.
Read more >Connecting Jest and Mongoose - Zell Liew
To connect to a MongoDB, you can use Mongoose's connect command. ... Safe to ignore. if (error.message === 'ns not found') return //...
Read more >[NODE-3597] Possible open handle in MongoClient.connect
npm test. > app2@1.0.0 test. > jest --detectOpenHandles. PASS __tests__/app2.spec.js. MongoDB Node.js Driver. ✓ should successfully connect ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem seems to be that the Async hooks module in Node doesn’t emit a
destroy
event for the “TLSWRAP” hook type.In Jest we have today something like:
These are all problematic hook types that are simply ignored. And “TLSWRAP” should probably be added to this list.
(Since there is always an underlying socket object which is what we’re really interested in.)
But to test this, we’d have to set up a TLS session object which is not exactly trivial.
I have the same issue with something as simple as this using axios
“axios”: “^0.21.4”, “jest”: “^27.2.4”, “ts-jest”: “^27.0.5”