Skip to content

refactor: flush microtask queue implementation #1374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/flush-micro-tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { setImmediate } from './helpers/timers';

export function flushMicroTasks() {
return new Promise((resolve) => setImmediate(resolve));
}

/**
* @deprecated To be removed in the next major release.
*/
type Thenable<T> = { then: (callback: () => T) => unknown };

/**
* This legacy implementation of `flushMicroTasks` is used for compatibility with
* older versions of React Native (pre 0.71) which uses Promise polyfil.
*
* For users with older version of React Native there is a workaround of using our own
* Jest preset instead the `react-native` one, but requiring such change would be a
* breaking change for existing users.
*
* @deprecated To be removed in the next major release.
*/
export function flushMicroTasksLegacy(): Thenable<void> {
return {
// using "thenable" instead of a Promise, because otherwise it breaks when
// using "modern" fake timers
then(resolve) {
setImmediate(resolve);
},
};
}
13 changes: 0 additions & 13 deletions src/flushMicroTasks.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cleanup } from './pure';
import { flushMicroTasks } from './flushMicroTasks';
import { flushMicroTasksLegacy } from './flush-micro-tasks';
import { getIsReactActEnvironment, setReactActEnvironment } from './act';

if (typeof process === 'undefined' || !process.env?.RNTL_SKIP_AUTO_CLEANUP) {
Expand All @@ -11,7 +11,7 @@ if (typeof process === 'undefined' || !process.env?.RNTL_SKIP_AUTO_CLEANUP) {
if (typeof afterEach === 'function') {
// eslint-disable-next-line no-undef
afterEach(async () => {
await flushMicroTasks();
await flushMicroTasksLegacy();
cleanup();
});
}
Expand Down
7 changes: 3 additions & 4 deletions src/waitFor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* globals jest */
import act, { setReactActEnvironment, getIsReactActEnvironment } from './act';
import { getConfig } from './config';
import { flushMicroTasks } from './flushMicroTasks';
import { flushMicroTasks, flushMicroTasksLegacy } from './flush-micro-tasks';
import { ErrorWithStack, copyStackTrace } from './helpers/errors';
import {
setTimeout,
clearTimeout,
setImmediate,
jestFakeTimersAreEnabled,
} from './helpers/timers';
import { checkReactVersionAtLeast } from './react-versions';
Expand Down Expand Up @@ -89,7 +88,7 @@ function waitForInternal<T>(
// of parallelization so we're fine.
// https://stackoverflow.com/a/59243586/971592
// eslint-disable-next-line no-await-in-loop
await new Promise((resolve) => setImmediate(resolve));
await flushMicroTasks();
}
} else {
overallTimeoutTimer = setTimeout(handleTimeout, timeout);
Expand Down Expand Up @@ -207,7 +206,7 @@ export default async function waitFor<T>(
try {
const result = await waitForInternal(expectation, optionsWithStackTrace);
// Flush the microtask queue before restoring the `act` environment
await flushMicroTasks();
await flushMicroTasksLegacy();
return result;
} finally {
setReactActEnvironment(previousActEnvironment);
Expand Down