Skip to content

Commit 4263696

Browse files
committed
refactor: restore original flush function as legacy version
1 parent 58a8073 commit 4263696

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/flushMicroTasks.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,28 @@ import { setImmediate } from './helpers/timers';
33
export function flushMicroTasks() {
44
return new Promise((resolve) => setImmediate(resolve));
55
}
6+
7+
/**
8+
* @deprecated To be removed in the next major release.
9+
*/
10+
type Thenable<T> = { then: (callback: () => T) => unknown };
11+
12+
/**
13+
* This legacy implementation of `flushMicroTasks` is used for compatibility with
14+
* older versions of React Native (pre 0.71) which uses Promise polyfil.
15+
*
16+
* For users with older version of React Native there is a workaround of using our own
17+
* Jest preset instead the `react-native` one, but requiring such change would be a
18+
* breaking change for existing users.
19+
*
20+
* @deprecated To be removed in the next major release.
21+
*/
22+
export function flushMicroTasksLegacy(): Thenable<void> {
23+
return {
24+
// using "thenable" instead of a Promise, because otherwise it breaks when
25+
// using "modern" fake timers
26+
then(resolve) {
27+
setImmediate(resolve);
28+
},
29+
};
30+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cleanup } from './pure';
2-
import { flushMicroTasks } from './flushMicroTasks';
2+
import { flushMicroTasksLegacy } from './flushMicroTasks';
33
import { getIsReactActEnvironment, setReactActEnvironment } from './act';
44

55
if (typeof process === 'undefined' || !process.env?.RNTL_SKIP_AUTO_CLEANUP) {
@@ -11,7 +11,7 @@ if (typeof process === 'undefined' || !process.env?.RNTL_SKIP_AUTO_CLEANUP) {
1111
if (typeof afterEach === 'function') {
1212
// eslint-disable-next-line no-undef
1313
afterEach(async () => {
14-
await flushMicroTasks();
14+
await flushMicroTasksLegacy();
1515
cleanup();
1616
});
1717
}

src/waitFor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* globals jest */
22
import act, { setReactActEnvironment, getIsReactActEnvironment } from './act';
33
import { getConfig } from './config';
4-
import { flushMicroTasks } from './flushMicroTasks';
4+
import { flushMicroTasks, flushMicroTasksLegacy } from './flushMicroTasks';
55
import { ErrorWithStack, copyStackTrace } from './helpers/errors';
66
import {
77
setTimeout,
@@ -206,7 +206,7 @@ export default async function waitFor<T>(
206206
try {
207207
const result = await waitForInternal(expectation, optionsWithStackTrace);
208208
// Flush the microtask queue before restoring the `act` environment
209-
await flushMicroTasks();
209+
await flushMicroTasksLegacy();
210210
return result;
211211
} finally {
212212
setReactActEnvironment(previousActEnvironment);

0 commit comments

Comments
 (0)