Skip to content

chore(breaking): deprecate flushMicrotasksQueue #340

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 3 commits into from
May 20, 2020
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ The [public API](https://callstack.github.io/react-native-testing-library/docs/a
- [`fireEvent`](https://callstack.github.io/react-native-testing-library/docs/api#fireevent) - invokes named event handler on the element.
- [`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.
- [`within`](https://callstack.github.io/react-native-testing-library/docs/api#within) - creates a queries object scoped for given element.
- [`flushMicrotasksQueue`](https://callstack.github.io/react-native-testing-library/docs/api#flushmicrotasksqueue) - waits for microtasks queue to flush.

## Migration Guides

Expand Down
15 changes: 15 additions & 0 deletions src/flushMicroTasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @flow

import { printDeprecationWarning } from './helpers/errors';

/**
* Wait for microtasks queue to flush
*/
export default function flushMicrotasksQueue(): Promise<any> {
printDeprecationWarning('flushMicrotasksQueue');
return flushMicroTasks();
}

export function flushMicroTasks(): Promise<any> {
return new Promise((resolve) => setImmediate(resolve));
}
7 changes: 0 additions & 7 deletions src/flushMicrotasksQueue.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/helpers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function printDeprecationWarning(functionName: string) {

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

warned[functionName] = true;
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import flushMicrotasksQueue from './flushMicrotasksQueue';
import { cleanup } from './pure';
import { flushMicroTasks } from './flushMicroTasks';

// If we're running in a test runner that supports afterEach
// then we'll automatically run cleanup afterEach test
Expand All @@ -10,7 +10,7 @@ import { cleanup } from './pure';
if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) {
// eslint-disable-next-line no-undef
afterEach(async () => {
await flushMicrotasksQueue();
await flushMicroTasks();
cleanup();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import act from './act';
import cleanup from './cleanup';
import fireEvent from './fireEvent';
import flushMicrotasksQueue from './flushMicrotasksQueue';
import flushMicrotasksQueue from './flushMicroTasks';
import render from './render';
import shallow from './shallow';
import waitFor, { waitForElement } from './waitFor';
Expand Down
6 changes: 5 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ export declare const render: (
options?: RenderOptions
) => RenderAPI;

export declare const flushMicrotasksQueue: () => Promise<any>;
export declare const cleanup: () => void;
export declare const fireEvent: FireEventAPI;
export declare const waitFor: WaitForFunction;
Expand All @@ -307,6 +306,11 @@ export declare const within: (instance: ReactTestInstance) => Queries;
*/
export declare const waitForElement: WaitForFunction;

/**
* @deprecated This function has been deprecated and will be removed in the next release.
*/
export declare const flushMicrotasksQueue: () => Promise<any>;

/**
* @deprecated This function has been removed.
*/
Expand Down
15 changes: 0 additions & 15 deletions website/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,21 +377,6 @@ Use cases for scoped queries include:
- queries scoped to a single item inside a FlatList containing many items
- queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation)

## `flushMicrotasksQueue`

Waits for microtasks queue to flush. Useful if you want to wait for some promises with `async/await`.

```jsx
import { flushMicrotasksQueue, render } from 'react-native-testing-library';

test('fetch data', async () => {
const { getByText } = render(<FetchData />);
getByText('fetch');
await flushMicrotasksQueue();
expect(getByText('fetch').props.title).toBe('loaded');
});
```

## `query` APIs

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:
Expand Down
12 changes: 12 additions & 0 deletions website/docs/Migration20.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ If you relied on setting `testID` for your custom components, you should probabl
:::caution
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).
:::

## Deprecated `flushMicrotasksQueue`

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.

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:

```js
function flushMicrotasksQueue() {
return new Promise((resolve) => setImmediate(resolve));
}
```