Skip to content

Commit

Permalink
feat(waitFor): Support async unstable_advanceTimersWrapper (#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed May 12, 2023
1 parent 39a64d4 commit d09b3c2
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/wait-for.js
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 () => {
// 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)
})
})
}
} else {
try {
Expand Down

0 comments on commit d09b3c2

Please sign in to comment.