Skip to content

Commit b595c74

Browse files
chore(breaking): deprecate flushMicrotasksQueue (#340)
* Deprecated flushMicrotasksQueue * Update website/docs/Migration20.md Co-authored-by: Michał Pierzchała <thymikee@gmail.com> * Renamed flushMicrotasksQueueInternal to flushMicroTasks Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
1 parent 4d0e448 commit b595c74

File tree

9 files changed

+36
-28
lines changed

9 files changed

+36
-28
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ The [public API](https://callstack.github.io/react-native-testing-library/docs/a
120120
- [`fireEvent`](https://callstack.github.io/react-native-testing-library/docs/api#fireevent) - invokes named event handler on the element.
121121
- [`waitFor`](https://callstack.github.io/react-native-testing-library/docs/api#waitfor) - waits for non-deterministic periods of time until your element appears or times out.
122122
- [`within`](https://callstack.github.io/react-native-testing-library/docs/api#within) - creates a queries object scoped for given element.
123-
- [`flushMicrotasksQueue`](https://callstack.github.io/react-native-testing-library/docs/api#flushmicrotasksqueue) - waits for microtasks queue to flush.
124123

125124
## Migration Guides
126125

src/flushMicroTasks.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @flow
2+
3+
import { printDeprecationWarning } from './helpers/errors';
4+
5+
/**
6+
* Wait for microtasks queue to flush
7+
*/
8+
export default function flushMicrotasksQueue(): Promise<any> {
9+
printDeprecationWarning('flushMicrotasksQueue');
10+
return flushMicroTasks();
11+
}
12+
13+
export function flushMicroTasks(): Promise<any> {
14+
return new Promise((resolve) => setImmediate(resolve));
15+
}

src/flushMicrotasksQueue.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/helpers/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function printDeprecationWarning(functionName: string) {
3333

3434
console.warn(`
3535
Deprecation Warning:
36-
${functionName} is not recommended for use and will be deleted in react-native-testing-library 2.x.
36+
Use of ${functionName} is not recommended and will be deleted in future versions of react-native-testing-library.
3737
`);
3838

3939
warned[functionName] = true;

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
2-
import flushMicrotasksQueue from './flushMicrotasksQueue';
32
import { cleanup } from './pure';
3+
import { flushMicroTasks } from './flushMicroTasks';
44

55
// If we're running in a test runner that supports afterEach
66
// then we'll automatically run cleanup afterEach test
@@ -10,7 +10,7 @@ import { cleanup } from './pure';
1010
if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) {
1111
// eslint-disable-next-line no-undef
1212
afterEach(async () => {
13-
await flushMicrotasksQueue();
13+
await flushMicroTasks();
1414
cleanup();
1515
});
1616
}

src/pure.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import act from './act';
33
import cleanup from './cleanup';
44
import fireEvent from './fireEvent';
5-
import flushMicrotasksQueue from './flushMicrotasksQueue';
5+
import flushMicrotasksQueue from './flushMicroTasks';
66
import render from './render';
77
import shallow from './shallow';
88
import waitFor, { waitForElement } from './waitFor';

typings/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ export declare const render: (
295295
options?: RenderOptions
296296
) => RenderAPI;
297297

298-
export declare const flushMicrotasksQueue: () => Promise<any>;
299298
export declare const cleanup: () => void;
300299
export declare const fireEvent: FireEventAPI;
301300
export declare const waitFor: WaitForFunction;
@@ -307,6 +306,11 @@ export declare const within: (instance: ReactTestInstance) => Queries;
307306
*/
308307
export declare const waitForElement: WaitForFunction;
309308

309+
/**
310+
* @deprecated This function has been deprecated and will be removed in the next release.
311+
*/
312+
export declare const flushMicrotasksQueue: () => Promise<any>;
313+
310314
/**
311315
* @deprecated This function has been removed.
312316
*/

website/docs/API.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -377,21 +377,6 @@ Use cases for scoped queries include:
377377
- queries scoped to a single item inside a FlatList containing many items
378378
- queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation)
379379

380-
## `flushMicrotasksQueue`
381-
382-
Waits for microtasks queue to flush. Useful if you want to wait for some promises with `async/await`.
383-
384-
```jsx
385-
import { flushMicrotasksQueue, render } from 'react-native-testing-library';
386-
387-
test('fetch data', async () => {
388-
const { getByText } = render(<FetchData />);
389-
getByText('fetch');
390-
await flushMicrotasksQueue();
391-
expect(getByText('fetch').props.title).toBe('loaded');
392-
});
393-
```
394-
395380
## `query` APIs
396381

397382
Each of the get APIs listed in the render section above have a complimentary query API. The get APIs will throw errors if a proper node cannot be found. This is normally the desired effect. However, if you want to make an assertion that an element is not present in the hierarchy, then you can use the query API instead:

website/docs/Migration20.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,15 @@ If you relied on setting `testID` for your custom components, you should probabl
113113
:::caution
114114
In general, you should avoid `byTestId` queries when possible and rather use queries that check things that can been seen by the user (e.g. `byText`, `byPlaceholder`, etc) or accessability queries (e.g. `byA11yHint`, `byA11yLabel`, etc).
115115
:::
116+
117+
## Deprecated `flushMicrotasksQueue`
118+
119+
We have deprecated `flushMicrotasksQueue` and plan to remove it in the next major version, as currently there are better alternatives available for helping you write async tests: `findBy` async queries and `waitFor` helper.
120+
121+
If you can't or don't want to migrate your tests, you can get rid of the deprecation warning by copy-pasting function's implementation into your project:
122+
123+
```js
124+
function flushMicrotasksQueue() {
125+
return new Promise((resolve) => setImmediate(resolve));
126+
}
127+
```

0 commit comments

Comments
 (0)