Skip to content

Open handle when using http.Server.listen #8554

@remcohaszing

Description

@remcohaszing

🐛 Bug Report

I think I ran into forwardemail/supertest#520. Trying to work around this, I got to a minimal example, which I think is actually a bug in Jest.

Jest detects open handles when an http server is started, even when it is closed afterwards.

To Reproduce

Create the following zero-configuration test case.

const http = require("http");

it("should not timeout", async () => {
  const server = http.createServer();
  await new Promise(resolve => {
    server.listen(() => {
      resolve();
    });
  });
  await new Promise((resolve, reject) => {
    server.close(err => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    });
  });
  // await new Promise(resolve => setTimeout(resolve, 0));
});

When jest is run using --detectOpenHandles, this will output the following:

$ jest --detectOpenHandles
 PASS  __tests__/index.js
  ✓ should not timeout (6ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.231s
Ran all test suites.

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

  ●  TCPSERVERWRAP

      4 |   const server = http.createServer();
      5 |   await new Promise(resolve => {
    > 6 |     server.listen(() => {
        |            ^
      7 |       resolve();
      8 |     });
      9 |   });

      at listen (__tests__/index.js:6:12)
      at Object.<anonymous>.it (__tests__/index.js:5:9)

However, when the “0 ms sleep” line at the end is uncommented, no open handles are detected.

Expected behavior

Jest should not detect any open handles.

Run npx envinfo --preset jest

  System:
    OS: Linux 4.15 Ubuntu 18.04.2 LTS (Bionic Beaver)
    CPU: (8) x64 Intel(R) Core(TM) i7-4720HQ CPU @ 2.60GHz
  Binaries:
    Node: 10.16.0 - /usr/bin/node
    Yarn: 1.16.0 - /usr/bin/yarn
    npm: 6.9.0 - ~/.local/bin/npm
  npmPackages:
    jest: ^24.8.0 => 24.8.0 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions