jest: DNSCHANNEL stops jest from closing

Jest does not closed himself after test finished

–detectOpenHandles suggest I need to close DNSCHANNEL manually:

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  DNSCHANNEL

      at dns.js:333:23

But I cannot find any additonal information based on that problem.

I have async tests running supertest and jest.

    "express": "^4.16.3",
    "jest": "^23.1.0",
    "supertest": "^3.1.0",
    "pg": "^7.4.3",
    "pg-pool": "^2.0.3"

Test code

const request = require('supertest');
const app = require('server');

// close the server after each test
afterEach(async () => {
  await app.db.close();
});

const wFixture = [{
      "complexity": 3,
}];

describe('Controllers', () => {
  test('list get method', async () => {
    const response = await request(app).get('/');
    expect(response.status).toEqual(200);
    expect(response.body.count).toEqual(1);
    expect(response.body.data).toEqual(wFixture);
  });
});

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 20
  • Comments: 20 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Can confirm that this issue still exists, in my case it does prevent jest to exit, thus requiring --forceExit:

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:32:24)
      at Object.<anonymous> (node_modules/got/dist/source/as-promise/types.js:14:16)

with all up-to-date versions:

- nock@13.0.4
- got@11.8.0
- jest@26.6.3
- node v14.15.0 (node@14 via brew)
- OSX 10.15.7

I’m seeing the same issue here.

In my opinion, the issue is between jest and the DNS Resolver class in NodeJS the standard library.

got is not to blame there. It’s simply a popular library using the Resolver class (which shouldn’t be instantiated by default in got as it is not used by default but that’s an issue for got).

@SimenB could it be possible to re-open this issue ?

const { Resolver } = require('dns').promises;

it('detects an open handle', () => {
  new Resolver();
  expect(true);
});

test.spec.ts

// https://nodejs.org/docs/latest-v12.x/api/dns.html#dns_dns_promises_api
import { promises } from 'dns';

test('Node.js DNS', () => {
  const { Resolver } = promises;

  const resolver = new Resolver();
  expect(typeof resolver).toBe('object');

  const servers = resolver.getServers();
  console.log(servers);
});

image

I haven’t found a way to close the open handle. ava does not cause an error.

For Googlers:

I have a file db.ts that I mocked with db.js. db.ts opens up a database connection and causes this error to appear when db.ts is ran in a Jest test.

Renaming the mock file to db.ts fixed this issue for me.

In order to solve this issue, I found two critical problems in a project and one antipattern we’ve been using. Hope this information will help everyone who faced same issue:

  • Instead of returning express().listener() we have been returning express() in server.js

Wrong version

const express = require('express');
const app = express();
app.listen(3000);
return app;

... in tests:
const server  = require('server');

Fixed version:

const express = require('express');
const app = express();
const server = app.listen(3000);
return server;

... in tests:
const server  = require('server');
  • Antipattern where we have global database object

Wrong version:

global.db = await db.create();

Fixed version:

app.db = await db.create();

... inside tests
afterAll(() => {
  server._events.request.db.close();
});

Same issue here. It is caused by mongodb in my case. This simple code will make it.

const { MongoClient } = require('mongodb');
test('test', () => {});

module version node : 10.4.0 mongodb : 3.0.10 jest : 23.1.0

I was seeing this with node v12.10.0, using got and nock. Upgrading to node v14.9.0 resolved it for me.

Even when i upgrade to v14.9.0 I still got this problem. I’m also using got in my project.

Did you change anything else ?

I was seeing this with node v12.10.0, using got and nock. Upgrading to node v14.9.0 resolved it for me.