Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Report] WaitUntil does not close handlers so jest stuck and waits #32

Open
npwork opened this issue Feb 9, 2022 · 8 comments
Open
Assignees
Labels
Bug Report This is a bug report

Comments

@npwork
Copy link

npwork commented Feb 9, 2022

Describe the bug
waitUntil does not close all open handlers so jest sits and waits

To Reproduce
Steps to reproduce the behavior:

  1. Create jest test like
const end = Date.now() + 1000
await waitUntil(() => Date.now() < end)
  1. Run test

You will see

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

Expected behavior
All handlers are closed

Screenshots
If you run with --detectOpenHandles you will see

Screen Shot 2022-02-09 at 6 55 06 PM

Additional context
Add any other context about the problem here.

@npwork npwork added the Bug Report This is a bug report label Feb 9, 2022
@npwork npwork changed the title [Bug Report] [Bug Report] WaitUntil does not close handlers so jest stuck and waits Feb 9, 2022
@aldeed
Copy link

aldeed commented Jun 12, 2022

Workaround is to disable timing out:

{
  timeout: Number.POSITIVE_INFINITY,
}

To fix this, the promise returned by delay() would need to be somehow cancellable based on the other promise resolving, but the way the code is currently written this would be a fairly big rewrite.

@devlato
Copy link
Owner

devlato commented Jun 13, 2022

@aldeed the problem's never-ending predicate support – and not the delay itself, since it's not called when the specified timeout is Infinity. I'll remove this test and infinite wait times from the documentation (but not from the implementation, to keep any existing consumers working).

@aldeed
Copy link

aldeed commented Jun 14, 2022

@devlato Maybe I'm not understanding you. I meant that adding an infinite timeout solved this for me because it then doesn't call delay() due to a specific check I noticed in the code. It's only an infinite timeout that can work around this issue because of that explicit check for it. Any actual timeout value causes creation of the delay promise, which is still unresolved at the time that the code exits.

The way I would normally fix this in the code is something like:

let otherPromiseResolved = false;

const delay = (delayMs) => {
  const start = Date.now();
  return new Promise((resolve) => {
    const interval = setInterval(() => {
      if (Date.now() - start > delayMs || otherPromiseResolved) {
        clearInterval(interval);
        resolve();
      }
    }, 500);
  )
}

// When the promise that `delay` is racing against resolves, set otherPromiseResolved = true.

The key point being to somehow inform delay that the promise it is racing against has resolved so that it can immediately resolve. But because there are several layers in this codebase, I wasn't sure how best to implement this pattern here.

@mgagliardo91
Copy link

@npwork Very much interested in a solution here or an alternative. This is blocking our test suite as well.

@NerdishShah
Copy link

@npwork @devlato This is been kept open for long, any progress made on this as its blocking us too. Thanks in advance

@devlato
Copy link
Owner

devlato commented Jan 26, 2023

@NerdishShah will try and fix it this weekend - thanks for reminding me!

@NerdishShah
Copy link

@NerdishShah will try and fix it this weekend - thanks for reminding me!

May I ask if there's any update on this?

@NerdishShah
Copy link

A gentle reminder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Report This is a bug report
Projects
None yet
Development

No branches or pull requests

5 participants