Skip to content

Commit d0da54c

Browse files
committed
fix: Prevent "missing act" warning for in-flight promises
1 parent b2f3968 commit d0da54c

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/pure.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ import act, {
1212
} from './act-compat'
1313
import {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+
1528
configureDTL({
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
}

0 commit comments

Comments
 (0)