File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,19 @@ 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+ Object . prototype . hasOwnProperty . call ( setTimeout , 'clock' )
22+ )
23+ } // istanbul ignore next
24+
25+ return false
26+ }
27+
1528configureDTL ( {
1629 unstable_advanceTimersWrapper : cb => {
1730 return act ( cb )
@@ -23,7 +36,21 @@ configureDTL({
2336 const previousActEnvironment = getIsReactActEnvironment ( )
2437 setReactActEnvironment ( false )
2538 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
2754 } finally {
2855 setReactActEnvironment ( previousActEnvironment )
2956 }
You can’t perform that action at this time.
0 commit comments