Promises makes jest test time out
See original GitHub issueHello 👋 Thank you for maintaining this project.
I believe I am experiencing a bug where awaiting a call to get response causes timeout.
Minimal example:
it("TIMEOUT when awaiting", async () => {
await supertest(app)
.get("/users/me")
});
it("TIMEOUT when using .then", (done) => {
supertest(app)
.get("/users/me")
.then(response => {
done();
});
});
it("WORKING when not using promises at all", () => {
supertest(app)
.get("/users/me");
});
The two first tests are failing with the following message:
Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:
Please let me know if I have left out any useful information.
All the best! Olav
Issue Analytics
- State:
- Created 3 years ago
- Reactions:21
- Comments:17
Top Results From Across the Web
Test Promise with timeout Jest - javascript - Stack Overflow
Jest allows you to "fake" the timers so test can run fast while still ensuring async code is behaving correctly. In your testing...
Read more >Promises makes jest test time out · Issue #697 · ladjs/supertest
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper...
Read more >Testing Asynchronous Code - Jest
Promises . Return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the...
Read more >Troubleshooting · Jest
If a promise doesn't resolve at all, this error might be thrown: - Error: Timeout - Async callback was not invoked within timeout...
Read more >How to test async functions with fake timers - Sinon.JS
With fake timers (lolex), testing code that depends on timers is easier, ... (timeout) => { return new Promise((resolve) => setTimeout(resolve, timeout)); ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m not sure this is related to the reported or known behaviour, but looked similar in my case and might help others stumbling over this.
For me jest.useFakeTimers() caused supertest to break existing test cases by timing out. I didn’t have time to dig into the cause of this, but given that fakeTimers is quite invasive I wouldn’t consider this a supertest/superagent bug.
On a project of mine, I was able to work around this by ensuring
listen
was not called as part of importing the Express app that is passed torequest
. Essentially, thelisten
call is done inindex.js
(our entrypoint) and the Express app is created inapp.js
.index.js
importsapp.js
. The tests only ever importapp.js
.