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