简体   繁体   中英

Testing express app with supertest timesout on delete and put test routes

I'm using supertest and jest to write my test. but the delete and put routes in particular don't work properly, it keeps waiting until it times out. My speculations tells me that the route is not being hit because i tried to log something in the route but it doesn't. So, i must be doing something wrong with supertest . Also, Im using methodoverride .

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

let server = request(app);

const aQuestionId = "5c78b8906c20cc5d22360a87";

test("DELETE /questions/:qID", done => {
    jest.setTimeout(10000);
    server
        // /questions/5c7899a24552624a5b9c7f35?_method=DELETE
        .delete(`/questions/${aQuestionId}`)
        .expect(200);
});

// Delete route
// DELETE /questions/:qID
router.delete("/questions/:qID", question.deleteQuestion);

The problem was that, I wasn't calling done when i complete my request which kept the connection open and then jest times out because request not closed

test("DELETE /questions/:qID", done => {
    jest.setTimeout(10000);
    server
        // /questions/5c7899a24552624a5b9c7f35?_method=DELETE
        .delete(`/questions/${aQuestionId}`)
        .expect(200, done); <= call done
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM