Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNSCHANNEL stops jest from closing #6423

Closed
presentsvlad opened this issue Jun 9, 2018 · 20 comments
Closed

DNSCHANNEL stops jest from closing #6423

presentsvlad opened this issue Jun 9, 2018 · 20 comments

Comments

@presentsvlad
Copy link

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);
  });
});
@linkenneth
Copy link

I'm seeing the same issue here.

@SimenB
Copy link
Member

SimenB commented Jun 9, 2018

We're not always able to point to user code - if it just shows dns it might be some polling or some such. Do you think you could put together a repo we could pull down showing the error?

@linkenneth
Copy link

Why is there DNS involved in filesystem polling anyways?

It's a bit difficult for me to isolate a repo for this, but it usually happens on functional tests with database setup in the beginning. My test itself looks like this:

import request from 'supertest';

import app from '../../app';
import knex from '../../knex';
import User from '../../models/user';

const userData = {/* ... */};

describe('/signup', () => {
  beforeEach(async () => {
    await knex.migrate.rollback();
    await knex.migrate.latest();
  });

  describe('GET', () => {
    test('should respond with 200 OK', async () => {
      await request(app).get('/signup').expect(200);
    });
  });

  describe('POST', () => {
    // TODO: create verifications and test these verifications
    test('should respond with 302 FOUND', async () => {
      await request(app).post('/signup').send(userData).expect(302);

      // TODO: test cookies set properly
    });

    test('should create the correct database entries', async () => {
      await request(app).post('/signup').send(userData);

      const users = await Promise.all([
        User.findByPrimaryEmail(userData.email),
        User.findByPrimaryPhone(userData.phone),
        User.findByPrimaryWeChat(userData.wechat),
        User.findByPrimaryQQ(userData.qq)
      ]);

      const userId = users[0].userId;
      expect(userId).toBeDefined();
      users.forEach((user) => expect(user.userId).toEqual(userId));
    });
  });
});

@NeoJRotary
Copy link

NeoJRotary commented Jun 18, 2018

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

@SimenB
Copy link
Member

SimenB commented Jun 18, 2018

I'll also point out that this does not mean there's a bug in Jest - the flag exists to help you track down what's not cleaned up in your own code. I'm therefore closing this issue.

I have an open PR for fixing handles opened up within a test, which is currently not working: #6263

I'd still be interested in an example showing just DNSCHANNEL as it'd be great if we could improve it.

@SimenB SimenB closed this as completed Jun 18, 2018
@vladyslav2
Copy link

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();
});

@Sun1ive
Copy link

Sun1ive commented Aug 22, 2018

same issue here.
only happens when db models included in tests

test:

const { agent } = require('supertest');
const createApp = require('../../createApp');
// const models = require('../../models');

describe('Testing base routes', () => {
  const app = agent(createApp());

  test('successfully create app', async () => {
    const res = await app.get('/test');
    expect(res.status).toBe(200);
  });

  test('expect message to be OK', async () => {
    const res = await app.get('/test');
    expect(res.body.message).toBe('OK');
  });
});

result

yarn run v1.9.4
$ cross-env NODE_ENV=test jest tests/src --detectOpenHandles --forceExit
 PASS  tests/src/index.spec.js
  Testing base routes
    √ successfully create app (62ms)
    √ expect message to be OK (11ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        3.426s, estimated 4s
Ran all test suites matching /tests\\src/i.
Done in 4.89s.

include db models (sequelize + postgres)

const { agent } = require('supertest');
const createApp = require('../../createApp');
const models = require('../../models');

describe('Testing base routes', () => {
  const app = agent(createApp());

  test('successfully create app', async ()=> {
    const res = await app.get('/test');
    expect(res.status).toBe(200);
  });

  test('expect message to be OK', async () => {
    const res = await app.get('/test');
    expect(res.body.message).toBe('OK');
  });
});

result:

$ yarn test
yarn run v1.9.4
$ cross-env NODE_ENV=test jest tests/src --detectOpenHandles --forceExit
 PASS  tests/src/index.spec.js
  Testing base routes
    √ successfully create app (62ms)
    √ expect message to be OK (12ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        4.681s
Ran all test suites matching /tests\\src/i.

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


  ●  DNSCHANNEL

      at dns.js:338:23

Done in 6.37s.

@SimenB
Copy link
Member

SimenB commented Aug 22, 2018

If you put together a reproduction showing it, I can take a look at if it's possible to improve the message

@Raikhen
Copy link

Raikhen commented Nov 23, 2018

Hey guys, does anyone know a solution for GraphQL Yoga? The server doesn't have a listen method, so we can't return it.

@johntran
Copy link

johntran commented Apr 4, 2019

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.

@CatsMiaow
Copy link

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.

@szmarczak
Copy link

I think it's a bug in Jest since the process exits normally.

@sennett
Copy link

sennett commented Aug 30, 2020

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

@jean-smaug
Copy link

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 ?

@sennett
Copy link

sennett commented Sep 1, 2020

@jean-smaug This works for me:

  it('calls nock', async () => {
    nock(process.env.CRM_DOMAIN)
      .get('/crm/api/v1.0/clients')
      .reply(200, clientFixture)

    const response = await got(`${process.env.CRM_DOMAIN}/crm/api/v1.0/clients`)
    expect(JSON.parse(response.body)[0].firstName).toEqual('From nock')
  })

All the versions:

  • nock@13.0.4
  • got@11.5.2
  • jest@26.4.2
  • node v14.9.0 (installed via nvm)
  • OSX 10.14.6

@mgcrea
Copy link

mgcrea commented Nov 10, 2020

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

kay-schecker added a commit to kay-schecker/oa-nest-middleware that referenced this issue Nov 11, 2020
@fungiboletus
Copy link

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);
});

@SimenB
Copy link
Member

SimenB commented Dec 5, 2020

@fungiboletus please open up a new issue - this has been closed for 2 years

@CatsMiaow
Copy link

It has been created as a new issue. #9982

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests