I'm writing tests for my app but I have stumbled on open handles problem.
I have tried all the solutions from google but still, I'm getting this error. Can you please help me?
I have tried closing all connections but still nothing, I don't know what I can do more. I'm having the same problem with job schedule
This is my tests
import request from 'supertest';
import mongoose from 'mongoose';
import { Server } from 'http';
import { app } from '../main';
import connectDB, { disconnect } from '../config/db';
let server: Server;
const globalAny: any = global;
beforeAll((done) => {
connectDB();
server = app.listen(4000, () => {
globalAny.agent = request.agent(server);
done();
});
});
afterAll(async () => {
console.log('closing');
server.close();
await mongoose.disconnect();
mongoose.connection.close();
await disconnect();
// @ts-ignore
await new Promise((resolve) => setTimeout(() => resolve(), 3000)); // avoid jest open handle error
});
it('logins into the app responds with token', (done) => {
request(server)
.post('/api/auth')
.send({ email: 'xxxx@xxx.com', password: 'xxxx$$' })
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
if (err) {
return done(err);
}
expect(res.body.success).toBe(true);
expect(res.body).toHaveProperty('token');
return done();
});
});
Error I'm getting
Jest has detected the following 3 open handles potentially keeping Jest from exiting:
● Timeout
302 |
303 | // 15:00 hours
> 304 | schedule.scheduleJob({ hour: 13, minute: 0 }, async () => {
| ^
305 | await hoursCa(15);
306 | });
307 | // 15:30 hours
at Timeout.Object.<anonymous>.Timeout.start (node_modules/long-timeout/index.js:41:20)
at new Timeout (node_modules/long-timeout/index.js:22:8)
at Object.<anonymous>.exports.setTimeout (node_modules/long-timeout/index.js:5:10)
● TLSWRAP
17 | }
18 | if (db) {
> 19 | await mongoose.connect(db);
| ^
20 | }
21 | console.log('MongoDB Connected...');
22 | } catch (err: any) {
at Object.resolveSRVRecord (node_modules/mongodb/src/connection_string.ts:78:7)
at Object.connect (node_modules/mongodb/src/operations/connect.ts:52:12)
● TLSWRAP
12 | let connection;
13 | if (db) {
> 14 | connection = mongoose.createConnection(db);
| ^
15 | autoIncrement.initialize(connection);
16 | }
17 | // const AutoIncrement = AutoIncrementFactory(connection);
at Object.resolveSRVRecord (node_modules/mongodb/src/connection_string.ts:78:7)
at Object.connect (node_modules/mongodb/src/operations/connect.ts:52:12)
at node_modules/mongodb/src/mongo_client.ts:417:7