Skip to content

feat(waitFor): Support async unstable_advanceTimersWrapper #1229

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

Merged
merged 1 commit into from
May 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/wait-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ function waitFor(
reject(error)
return
}
// we *could* (maybe should?) use `advanceTimersToNextTimer` but it's
// possible that could make this loop go on forever if someone is using
// third party code that's setting up recursive timers so rapidly that
// the user's timer's don't get a chance to resolve. So we'll advance
// by an interval instead. (We have a test for this case).
advanceTimersWrapper(() => {

// In this rare case, we *need* to wait for in-flight promises
// to resolve before continuing. We don't need to take advantage
// of parallelization so we're fine.
// https://stackoverflow.com/a/59243586/971592
// eslint-disable-next-line no-await-in-loop
await advanceTimersWrapper(async () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious as an outsider, why is await and async needed here when we don't explicitly await anything? Are we trying to wait a tick?

// we *could* (maybe should?) use `advanceTimersToNextTimer` but it's
// possible that could make this loop go on forever if someone is using
// third party code that's setting up recursive timers so rapidly that
// the user's timer's don't get a chance to resolve. So we'll advance
// by an interval instead. (We have a test for this case).
jest.advanceTimersByTime(interval)
})

Expand All @@ -85,18 +91,6 @@ function waitFor(
if (finished) {
break
}

// In this rare case, we *need* to wait for in-flight promises
// to resolve before continuing. We don't need to take advantage
// of parallelization so we're fine.
// https://stackoverflow.com/a/59243586/971592
// eslint-disable-next-line no-await-in-loop
await advanceTimersWrapper(async () => {
await new Promise(r => {
setTimeout(r, 0)
jest.advanceTimersByTime(0)
Comment on lines -96 to -97

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if this was a bug but shouldn't the order of these 2 statement been reversed? ie. the promise should resolve after advancing timers?

})
})
}
} else {
try {
Expand Down