question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Open handles when starting and stopping an http server in a test.

See original GitHub issue

🐛 Bug Report

Starting an http server results in an open handle despite closing the server down after tests ran.

To Reproduce

Steps to reproduce the behavior:

Create the following file named demo.test.js:

const http = require('http');

describe('demo', () => {
    let server;

    beforeAll(() => {
        server = http.createServer((req, res) => {
            res.write('ok');
            res.end();
        });
        server.listen();
    });

    afterAll(async() => {
        await server.close();
    });

    test('my test', async () => {

    });
});

Running with jest produces the following output

$ npx jest --detectOpenHandles .
 PASS  ./demo.test.js
  demo
    ✓ my test (2ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.759s
Ran all test suites matching /./i.

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

  ●  TCPSERVERWRAP

      10 |             res.end();
      11 |         });
    > 12 |         server.listen();
         |                ^
      13 |     });
      14 | 
      15 |     afterAll(() => {

      at Object.listen (demo.test.js:12:16)

Expected behavior

No open handles should be left.

Run npx envinfo --preset jest

Paste the results here:

$ npx envinfo --preset jest
npx: installed 1 in 1.938s

  System:
    OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
    CPU: x64 Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz
  Binaries:
    Node: 10.8.0 - ~/.nvm/versions/node/v10.8.0/bin/node
    npm: 6.2.0 - ~/.nvm/versions/node/v10.8.0/bin/npm
  npmPackages:
    jest: ^23.5.0 => 23.5.0 

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9

github_iconTop GitHub Comments

21reactions
pascalopitzcommented, Aug 29, 2018

Just to follow this up, should anybody read this because they’re testing with supertest and express. The following leaves no open handles.

import supertest from 'supertest';
import express from 'express';
import http from 'http';

describe('demo test', () => {
    let appp, server;

    beforeAll(done => {
        app = new express();
        server = http.createServer(app);
        server.listen(done);
    });

    afterAll(done => {
        server.close(done);
    });

    it('returns 500', async () => {
        const response = await supertest(app).get('/');
        expect(response.status).toBe(500);
    });
});
18reactions
SimenBcommented, Aug 29, 2018

That’s because you don’t wait for it to close down. The http API is callback based, not promise.

This successfully closes down without a warning from detectOpenHandles:

const http = require('http');

describe('demo', () => {
  let server;

  beforeAll(done => {
    server = http.createServer((req, res) => {
      res.write('ok');
      res.end();
    });
    server.listen(done);
  });

  afterAll(done => {
    server.close(done);
  });

  test('my test', async () => {});
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

go - How to stop http.ListenAndServe() - Stack Overflow
The problem is that in my application the HTTP server is only one component and it is required to stop and start at...
Read more >
Graceful shutdown in Go http server | by Sam Wang - Medium
When backend developers writing http servers, the most usual question is: how am I being able to terminate all the running processes and ......
Read more >
HTTP server - Learn Go with tests - GitBook
This will start a web server listening on a port, creating a goroutine for every request and running it against a Handler ....
Read more >
http.Server.Shutdown() crashing? - Google Groups
This is the output after visiting http://localhost:8080/stop on my browser. 2017/07/09 13:58:40 Server starting up. ... Println("test - before shutdown").
Read more >
How do you make sure your website works properly?
stopped working, or the web server itself has a configuration problem. Usually it's best to resort to your hosting provider's support team. 503: ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found