I'm programming server in Node.js v12.18.3, using Express framework. My code uses Jest for unit testing. In one of my module, I've used the got package. But it's not used in any unit test.
I've designed the following minimal test:
const { app } = require('../app');
const { mongoose, database } = require('../database/database');
const api_dict = require("../api_dictionary");
var User = require('../models/user');
describe("===== POST " + api_dict.users_create + " Tests ===== ", () => {
var db;
beforeAll(async () => {
await mongoose.connect(database, { useNewUrlParser: true, useUnifiedTopology: true });
db = mongoose.connection;
});
afterAll(async () => {
await db.close();
});
test("Create user", () => {
});
});
I'm using jest --detectOpenHandles command to detect open handles. Jest detected one:
npm test
> jetfire@0.0.0 test E:\Projects\github\project_name
> cross-env NODE_ENV=test jest --detectOpenHandles
PASS tests/tests.js (8.118 s)
===== POST /user/create Tests =====
√ Create user (1 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 8.196 s
Ran all test suites.
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● DNSCHANNEL
at new CacheableLookup (node_modules/cacheable-lookup/source/index.js:75:14)
at Object.<anonymous> (node_modules/got/dist/source/core/index.js:29:24)
at Object.<anonymous> (node_modules/got/dist/source/as-promise/types.js:16:16)
How to ensure that this handle is closed when all unit tests are ran?