@@ -63,22 +63,28 @@ function getFakeTimersConfigFromType(type: FakeTimersTypes) {
6363
6464const jestFakeTimersAreEnabled = ( ) : boolean => Boolean ( getJestFakeTimersType ( ) ) ;
6565
66- // we only run our tests in node, and setImmediate is supported in node.
67- function setImmediatePolyfill ( fn : ( ) => void ) {
68- return globalObj . setTimeout ( fn , 0 ) ;
69- }
70-
7166type BindTimeFunctions = {
7267 clearTimeoutFn : typeof clearTimeout ;
7368 setImmediateFn : typeof setImmediate ;
7469 setTimeoutFn : typeof setTimeout ;
7570} ;
7671
7772function bindTimeFunctions ( ) : BindTimeFunctions {
73+ // Capture the current (real) `setTimeout` so the `setImmediate` polyfill keeps
74+ // using real timers even after fake timers are re-applied. Reading
75+ // `globalObj.setTimeout` lazily inside the polyfill would pick up fake timers.
76+ const realSetTimeout = globalObj . setTimeout ;
77+
78+ // `setImmediate` exists in Node, but not in environments such as jsdom, so we
79+ // fall back to a `setTimeout`-based polyfill there (see #1767).
80+ function setImmediatePolyfill ( fn : ( ) => void ) {
81+ return realSetTimeout ( fn , 0 ) ;
82+ }
83+
7884 return {
7985 clearTimeoutFn : globalObj . clearTimeout ,
8086 setImmediateFn : globalObj . setImmediate || setImmediatePolyfill ,
81- setTimeoutFn : globalObj . setTimeout ,
87+ setTimeoutFn : realSetTimeout ,
8288 } ;
8389}
8490
0 commit comments