File tree 2 files changed +44
-4
lines changed
2 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -102,9 +102,27 @@ describe.each([
102
102
React . useEffect ( ( ) => {
103
103
if ( fetchState . fetching ) {
104
104
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
+ )
108
126
} )
109
127
}
110
128
} , [ fetchState ] )
Original file line number Diff line number Diff line change @@ -12,6 +12,20 @@ import act, {
12
12
} from './act-compat'
13
13
import { fireEvent } from './fire-event'
14
14
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
+
15
29
configureDTL ( {
16
30
unstable_advanceTimersWrapper : cb => {
17
31
return act ( cb )
@@ -27,7 +41,15 @@ configureDTL({
27
41
// Drain microtask queue.
28
42
// Otherwise we'll restore the previous act() environment, before we resolve the `waitFor` call.
29
43
// 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
+ } )
31
53
32
54
return result
33
55
} finally {
You can’t perform that action at this time.
0 commit comments