Skip to content

Commit bfd1db7

Browse files
committed
Now I member
1 parent d935fb1 commit bfd1db7

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/__tests__/end-to-end.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,27 @@ describe.each([
102102
React.useEffect(() => {
103103
if (fetchState.fetching) {
104104
fetchAMessageInAMicrotask().then(res => {
105-
return res.json().then(data => {
106-
setFetchState({todo: data.title, fetching: false})
107-
})
105+
return (
106+
res
107+
.json()
108+
// By spec, the runtime can only yield back to the event loop once
109+
// the microtask queue is empty.
110+
// So we ensure that we actually wait for that as well before yielding back from `waitFor`.
111+
.then(data => data)
112+
.then(data => data)
113+
.then(data => data)
114+
.then(data => data)
115+
.then(data => data)
116+
.then(data => data)
117+
.then(data => data)
118+
.then(data => data)
119+
.then(data => data)
120+
.then(data => data)
121+
.then(data => data)
122+
.then(data => {
123+
setFetchState({todo: data.title, fetching: false})
124+
})
125+
)
108126
})
109127
}
110128
}, [fetchState])

src/pure.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ import act, {
1212
} from './act-compat'
1313
import {fireEvent} from './fire-event'
1414

15+
function jestFakeTimersAreEnabled() {
16+
/* istanbul ignore else */
17+
if (typeof jest !== 'undefined' && jest !== null) {
18+
return (
19+
// legacy timers
20+
setTimeout._isMockFunction === true || // modern timers
21+
// eslint-disable-next-line prefer-object-has-own -- No Object.hasOwn in all target environments we support.
22+
Object.prototype.hasOwnProperty.call(setTimeout, 'clock')
23+
)
24+
} // istanbul ignore next
25+
26+
return false
27+
}
28+
1529
configureDTL({
1630
unstable_advanceTimersWrapper: cb => {
1731
return act(cb)
@@ -27,7 +41,15 @@ configureDTL({
2741
// Drain microtask queue.
2842
// Otherwise we'll restore the previous act() environment, before we resolve the `waitFor` call.
2943
// The caller would have no chance to wrap the in-flight Promises in `act()`
30-
await Promise.resolve().then(() => {})
44+
await new Promise(resolve => {
45+
setTimeout(() => {
46+
resolve()
47+
}, 0)
48+
49+
if (jestFakeTimersAreEnabled()) {
50+
jest.advanceTimersByTime(0)
51+
}
52+
})
3153

3254
return result
3355
} finally {

0 commit comments

Comments
 (0)