File tree 1 file changed +28
-1
lines changed
1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,19 @@ 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
+ Object . prototype . hasOwnProperty . call ( setTimeout , 'clock' )
22
+ )
23
+ } // istanbul ignore next
24
+
25
+ return false
26
+ }
27
+
15
28
configureDTL ( {
16
29
unstable_advanceTimersWrapper : cb => {
17
30
return act ( cb )
@@ -23,7 +36,21 @@ configureDTL({
23
36
const previousActEnvironment = getIsReactActEnvironment ( )
24
37
setReactActEnvironment ( false )
25
38
try {
26
- return await cb ( )
39
+ const result = await cb ( )
40
+ // Drain microtask queue.
41
+ // Otherwise we'll restore the previous act() environment, before we resolve the `waitFor` call.
42
+ // The caller would have no chance to wrap the in-flight Promises in `act()`
43
+ await new Promise ( resolve => {
44
+ setTimeout ( ( ) => {
45
+ resolve ( )
46
+ } , 0 )
47
+
48
+ if ( jestFakeTimersAreEnabled ( ) ) {
49
+ jest . advanceTimersByTime ( 0 )
50
+ }
51
+ } )
52
+
53
+ return result
27
54
} finally {
28
55
setReactActEnvironment ( previousActEnvironment )
29
56
}
You can’t perform that action at this time.
0 commit comments