File tree Expand file tree Collapse file tree 2 files changed +44
-4
lines changed Expand file tree Collapse file tree 2 files changed +44
-4
lines changed Original file line number Diff line number Diff 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 ] )
Original file line number Diff line number Diff line change @@ -12,6 +12,20 @@ import act, {
1212} from './act-compat'
1313import { 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+
1529configureDTL ( {
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 {
You can’t perform that action at this time.
0 commit comments