Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here are my HTTP routes

 app.get('/', (req, res) => {
    res.status(200).send('Hello World!')
})

app.post('/sample', (req, res) => {
    res.status(200).json({
        x:1,y:2
    });
})

I would like to test for the following

1) GET request working fine.

2)the /sample response contains the properties and x and y

const request = require('supertest');
const app = require('../app');

describe('Test the root path', () => {
    test('It should response the GET method', () => {
        return request(app).get('/').expect(200);
    });
})

describe('Test the post path', () => {
    test('It should response the POST method', (done) => {
        return request(app).post('/sample').expect(200).end(err,data=>{
            expect(data.body.x).toEqual('1');

        });
    });
})

But I got the following error on running the test

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

return request(app).get('/').expect(200);

like image 613
shellakkshellu Avatar asked Oct 08 '18 11:10

shellakkshellu


2 Answers

you need to call done() in the end() method

const request = require("supertest");
const app = require("../app");

let server = request(app);

it("should return 404", done =>
    server
        .get("/")
        .expect(404)
        .end(done);
});

like image 141
Nenoj Avatar answered Sep 29 '22 09:09

Nenoj


This trick worked;

afterAll(async () => {
    await new Promise(resolve => setTimeout(() => resolve(), 500)); // avoid jest open handle error
});

As described in this github issue.

like image 23
David Okwii Avatar answered Sep 29 '22 09:09

David Okwii