supertest
supertest copied to clipboard
POST, Promise never resolves
I am doing this unit test with supertest and chai and I do it in these two way
it("POST /novedades/api/postSolicitudVacaciones (servicio de guardar una solicitud de vacaciones) ", (done) => {
request(app)
.post("/novedades/api/postSolicitudVacaciones")
.send({
dtFechaInicio: "2021-07-30",
intNumeroDiasDisfrutar: 3,
intNumeroDiasCompensar: 0,
arrAprobadores: "[email protected]"
})
.set({ token: token, Accept: "application/json" })
.expect((res) => {
console.log(res)
expect(res.body).to.have.property("error", false);
})
.expect(200)
.end((err) => {
if (err) throw new Error(err)
})
})
it("POST /novedades/api/postSolicitudVacaciones (servicio de guardar una solicitud de vacaciones) ", async() => {
await agent
.post("/novedades/api/postSolicitudVacaciones")
.send({
dtFechaInicio: "2021-07-30",
intNumeroDiasDisfrutar: 3,
intNumeroDiasCompensar: 0,
arrAprobadores: "[email protected]"
})
.set({ token: token, Accept: "application/json" })
.expect("Content-Type", /json/)
.expect(200)
.then((err, res) => {
console.log(res.body);
expect(res.body).to.have.property("error", false);
})
});
And in both cases I always get the error 'Timeout of 2000ms exceeded. For async tests and hooks, ensure "done ()" is called; if returning a Promise, ensure it resolves. ' and the test performs the unit test well because he saves in BD
Same error here, i tried to increase with jest.timeOut(5000) but it's same while my GET routes work well
jest.setTimeout(50000)
it('ALORS la [reponse] doit retourner le [code de status] : 200', async() => {
const {statusCode} = await request.post("/api/v1/word/create")
.send({
category_id: 1,
title: "test",
description: "description longue",
descriptionShort: "description courte"
})
expect(statusCode).toBe(200)
})
Did you solve this? Having the same problem
Make sure the actual API works. Was having the same problem with an exception uncatched inside main application. Would not show in log so I was thinking all this async stuff was going wrong.
Finally I had to resolve to use supertest like this for async await to work. const supertestInstance = require('supertest')("http://localhost:3000/api");
Otherwise if you consume the app directly the async calls are SLOW/crawled by something which I did not find. With a long enough timeout I could see the post async test beeing receive minutes later on the supertest logs.
Having the same issue. A post request with an attachment. Just does not finish the test. I tried a 200 second timeout.. didn't work.
Anyone figured this out?
it('/upload returns a cached file location', () => {
let testFileLocation = `${__dirname}/assets/test_image.jpeg`;
return request(app.getHttpServer()).post('/upload')
.attach('file', testFileLocation)
.expect(201)
})
Error:
FAIL test/app.e2e-spec.ts (144.524 s)
● AppController (e2e) › /upload returns a cached file location
thrown: "Exceeded timeout of 60000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
28 | })
29 |
> 30 | it('/upload returns a cached file location', () => {
| ^
31 | let testFileLocation = `${__dirname}/assets/test_image.jpeg`;
32 | return request(app.getHttpServer()).post('/upload')
33 | .attach('file', testFileLocation)
at app.e2e-spec.ts:30:3
at Object.<anonymous> (app.e2e-spec.ts:7:1)
● Test suite failed to run
thrown: "Exceeded timeout of 60000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
24 | })
25 |
> 26 | afterAll(async () => {
| ^
27 | await app.close()
28 | })
29 |
at app.e2e-spec.ts:26:3
at Object.<anonymous> (app.e2e-spec.ts:7:1)
I am having the same issue.
+1, so is there any workaround?