Skip to content

Commit

Permalink
refactor: flush microtask queue implementation (#1374)
Browse files Browse the repository at this point in the history
* refactor: flush microtask queue implementation

* refactor: code review changes

* refactor: restore original flush function as legacy version

* chore: rename file
  • Loading branch information
mdjastrzebski committed Jun 9, 2023
1 parent ce75f66 commit 918be8c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
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

0 comments on commit 918be8c

Please sign in to comment.