loopbackio / loopbackio/loopback-next
Jest detects open handles when disconnecting a MongoDB datasource
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1.1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 27
Description
### Describe the bug
When closing a connection to a MongoDB database, Jest reports that there are still open handles.
Packages
```
"dependencies": {
"@loopback/repository": "^5.1.4",
"loopback-connector-mongodb": "^6.2.0"
},
"devDependencies": {
"jest": "^29.5.0"
}
```
Test example
```
const {juggler} = require("@loopback/repository");
describe('Should close without open handles', () => {
let datasource;
beforeAll(async () => {
datasource = new juggler.DataSource({
name: "mongo",
connector: "mongodb",
url: "",
host: "127.0.0.1",
port: 27017,
user: "",
password: "",
database: "customers",
useNewUrlParser: true,
authSource: "admin",
})
await datasource.connect();
});
afterAll(async () => {
await datasource.disconnect();
await datasource.stop();
});
it('Handles are still open', () => {
expect(true).toBe(true);
});
});
```
This does not happen with a memory data store (as I assume there is no actual connection).
### Expectation
The test completes without any errors/warnings
### Reality
Jest complains that there are still open handles
```
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● TCPWRAP
6 |
7 | beforeAll(async () => {
> 8 | datasource = new juggler.DataSource({
| ^
9 | name: "mongo",
10 | connector: "mongodb",
11 | url: "",
at makeConnection (node_modules/mongodb/lib/core/connection/connect.js:310:20)
at Array.reduce ()
at Object. (__tests__/example.test.js:8:22)
```
### Repository with minimal reproducible error
https://github.com/MattLishmanCtM/lb-connection-error
Node version: v18.12.0
OS: MacOS Ventura
Is there something I am missing about this?
Is there some other form of disconnecting/stopping that I should be doing?
I realise the officially supported package for LoopBack as a whole is Mocha but I need to use the repository package as a standalone with Jest.
### Logs
_No response_
### Additional information
_No response_
### Reproduction
https://github.com/MattLishmanCtM/lb-connection-error
Contributor guide
Research direction
Start with the minimal reproduction and __tests__/example.test.js, then inspect the datasource.connect(), disconnect(), and stop() lifecycle used in the test. Reproduce the Jest run with the MongoDB datasource and verify that the test exits without the reported TCPWRAP open-handle warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, node.js
- Domain
- backend, database, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100