4

I'm trying to test a very basic API made in Node + Express with Jest and Supertest but I'm getting the following output warning:

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

  ●  TCPSERVERWRAP

    > 34 | const server = app.listen(PORT, () => {
         |                    ^
      35 |     console.log(`Server is running on port ${PORT}.`);
      36 | });
      37 | 

My server is defined like this on the server entry JS file:

const server = app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}.`);
});

module.exports = server;

And my test file looks like this:

describe('Users endpoint', () => {
    let server;

    beforeEach(() => { server = require('../server'); });
    afterEach((done) => { server.close(done); });

    it('should create new user if we do a POST', (done) => {
        request(server)
            .get('/api/users')
            .expect(200)
            .end(done);
    });
});

As far as I know, calling server.close() passing the done argument on afterEach function should ensure the server closes after each unit test, but it doesn't.

Am I missing something?

ALREADY FOUND A SOLUTION

After adding a new test case besides the only one I had and modify the afterEach function removing the done argument like this:

afterEach(() => { server.close(); });

the open handle warning doesn't show anymore.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
Marc Hernández
  • 318
  • 1
  • 7

1 Answers1

0

My server is defined like you. But I wanted to kill JEST after all my test where passed.

If someone wanna do the same

On the top of you file.test.js add

const server = require('../app');

And at the end

afterAll((done) => server.close(done));
crg
  • 4,284
  • 2
  • 29
  • 57