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.