Skip to content

Commit

Permalink
Now I member
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Feb 16, 2023
1 parent d935fb1 commit bfd1db7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/__tests__/end-to-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,27 @@ describe.each([
React.useEffect(() => {
if (fetchState.fetching) {
fetchAMessageInAMicrotask().then(res => {
return res.json().then(data => {
setFetchState({todo: data.title, fetching: false})
})
return (
res
.json()
// By spec, the runtime can only yield back to the event loop once
// the microtask queue is empty.
// So we ensure that we actually wait for that as well before yielding back from `waitFor`.
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => data)
.then(data => {
setFetchState({todo: data.title, fetching: false})
})
)
})
}
}, [fetchState])
Expand Down
24 changes: 23 additions & 1 deletion src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import act, {
} from './act-compat'
import {fireEvent} from './fire-event'

function jestFakeTimersAreEnabled() {
/* istanbul ignore else */
if (typeof jest !== 'undefined' && jest !== null) {
return (
// legacy timers
setTimeout._isMockFunction === true || // modern timers
// eslint-disable-next-line prefer-object-has-own -- No Object.hasOwn in all target environments we support.
Object.prototype.hasOwnProperty.call(setTimeout, 'clock')
)
} // istanbul ignore next

return false
}

configureDTL({
unstable_advanceTimersWrapper: cb => {
return act(cb)
Expand All @@ -27,7 +41,15 @@ configureDTL({
// Drain microtask queue.
// Otherwise we'll restore the previous act() environment, before we resolve the `waitFor` call.
// The caller would have no chance to wrap the in-flight Promises in `act()`
await Promise.resolve().then(() => {})
await new Promise(resolve => {
setTimeout(() => {
resolve()
}, 0)

if (jestFakeTimersAreEnabled()) {
jest.advanceTimersByTime(0)
}
})

return result
} finally {
Expand Down

0 comments on commit bfd1db7

Please sign in to comment.