question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Promises makes jest test time out

See original GitHub issue

Hello 👋 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:open
  • Created 3 years ago
  • Reactions:21
  • Comments:17

github_iconTop GitHub Comments

1reaction
mojadevcommented, Jun 5, 2022

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.

0reactions
LoganBarnettcommented, Nov 11, 2022

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 to request. Essentially, the listen call is done in index.js (our entrypoint) and the Express app is created in app.js. index.js imports app.js. The tests only ever import app.js.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found