Skip to content

refactor: remove remaining support for old async utils #706

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 9 commits into from
Jan 5, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<a href="https://testing-library.com/">
<img width="150" height="150" src="https://raw.githubusercontent.com/testing-library/dom-testing-library/master/other/octopus.png">
</a>

<h1>eslint-plugin-testing-library</h1>
<p>ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library</p>
</div>
Expand Down Expand Up @@ -222,7 +223,6 @@ To enable this configuration use the `extends` property in your
| [no-promise-in-fire-event](docs/rules/no-promise-in-fire-event.md) | Disallow the use of promises passed to a `fireEvent` method | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | | |
| [no-render-in-lifecycle](docs/rules/no-render-in-lifecycle.md) | Disallow the use of `render` in testing frameworks setup functions | ![badge-angular][] ![badge-marko][] ![badge-react][] ![badge-vue][] | | |
| [no-unnecessary-act](docs/rules/no-unnecessary-act.md) | Disallow wrapping Testing Library utils or empty callbacks in `act` | ![badge-marko][] ![badge-react][] | | |
| [no-wait-for-empty-callback](docs/rules/no-wait-for-empty-callback.md) | Disallow empty callbacks for `waitFor` and `waitForElementToBeRemoved` | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | | |
| [no-wait-for-multiple-assertions](docs/rules/no-wait-for-multiple-assertions.md) | Disallow the use of multiple `expect` calls inside `waitFor` | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | | |
| [no-wait-for-side-effects](docs/rules/no-wait-for-side-effects.md) | Disallow the use of side effects in `waitFor` | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | | |
| [no-wait-for-snapshot](docs/rules/no-wait-for-snapshot.md) | Ensures no snapshot is generated inside of a `waitFor` call | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | | |
Expand Down
2 changes: 2 additions & 0 deletions docs/migration-guides/v6.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ If you are not on v5 yet, we recommend first following the [v5 migration guide](
## Overview

- `prefer-wait-for` was removed
- `no-wait-for-empty-callback` was removed
- `await-fire-event` is now called `await-async-events` with support for an `eventModule` option with `userEvent` and/or `fireEvent`
- `await-async-events` is now enabled by default for `fireEvent` in Vue and Marko shared configs
- `await-async-events` is now enabled by default for `userEvent` in all shared configs
Expand All @@ -21,6 +22,7 @@ If you are not on v5 yet, we recommend first following the [v5 migration guide](
## Steps to upgrade

- Removing `testing-library/prefer-wait-for` if you were referencing it manually somewhere
- Removing `testing-library/no-wait-for-empty-callback` if you were referencing it manually somewhere
- Renaming `testing-library/await-fire-event` to `testing-library/await-async-events` if you were referencing it manually somewhere
- Renaming `testing-library/await-async-query` to `testing-library/await-async-queries` if you were referencing it manually somewhere
- Renaming `testing-library/no-await-async-query` to `testing-library/no-await-async-queries` if you were referencing it manually somewhere
Expand Down
5 changes: 1 addition & 4 deletions docs/rules/await-async-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ Ensure that promises returned by async utils are handled properly.

Testing library provides several utilities for dealing with asynchronous code. These are useful to wait for an element until certain criteria or situation happens. The available async utils are:

- `waitFor` _(introduced since dom-testing-library v7)_
- `waitFor`
- `waitForElementToBeRemoved`
- `wait` _(**deprecated** since dom-testing-library v7)_
- `waitForElement` _(**deprecated** since dom-testing-library v7)_
- `waitForDomChange` _(**deprecated** since dom-testing-library v7)_

This rule aims to prevent users from forgetting to handle the returned
promise from async utils, which could lead to
Expand Down
45 changes: 0 additions & 45 deletions docs/rules/no-wait-for-empty-callback.md

This file was deleted.

8 changes: 0 additions & 8 deletions docs/rules/no-wait-for-snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ const bar = async () => {
await waitFor(() => expect(container).toMatchInlineSnapshot());
// ...
};

const baz = async () => {
// ...
await wait(() => {
expect(container).toMatchSnapshot();
});
// ...
};
```

Examples of **correct** code for this rule:
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/prefer-find-by.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

## Rule details

This rule aims to use `findBy*` or `findAllBy*` queries to wait for elements, rather than using `waitFor`, or the deprecated methods `waitForElement` and `wait`.
This rule aims to use `findBy*` or `findAllBy*` queries to wait for elements, rather than using `waitFor`.
This rule analyzes those cases where `waitFor` is used with just one query method, in the form of an arrow function with only one statement (that is, without a block of statements). Given the callback could be more complex, this rule does not consider function callbacks or arrow functions with blocks of code.

Examples of **incorrect** code for this rule
Expand Down
1 change: 0 additions & 1 deletion lib/configs/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export = {
'testing-library/no-node-access': 'error',
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-render-in-lifecycle': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/no-wait-for-multiple-assertions': 'error',
'testing-library/no-wait-for-side-effects': 'error',
'testing-library/no-wait-for-snapshot': 'error',
Expand Down
1 change: 0 additions & 1 deletion lib/configs/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export = {
'testing-library/no-global-regexp-flag-in-query': 'error',
'testing-library/no-node-access': 'error',
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/no-wait-for-multiple-assertions': 'error',
'testing-library/no-wait-for-side-effects': 'error',
'testing-library/no-wait-for-snapshot': 'error',
Expand Down
1 change: 0 additions & 1 deletion lib/configs/marko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export = {
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-render-in-lifecycle': 'error',
'testing-library/no-unnecessary-act': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/no-wait-for-multiple-assertions': 'error',
'testing-library/no-wait-for-side-effects': 'error',
'testing-library/no-wait-for-snapshot': 'error',
Expand Down
1 change: 0 additions & 1 deletion lib/configs/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export = {
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-render-in-lifecycle': 'error',
'testing-library/no-unnecessary-act': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/no-wait-for-multiple-assertions': 'error',
'testing-library/no-wait-for-side-effects': 'error',
'testing-library/no-wait-for-snapshot': 'error',
Expand Down
1 change: 0 additions & 1 deletion lib/configs/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export = {
'testing-library/no-node-access': 'error',
'testing-library/no-promise-in-fire-event': 'error',
'testing-library/no-render-in-lifecycle': 'error',
'testing-library/no-wait-for-empty-callback': 'error',
'testing-library/no-wait-for-multiple-assertions': 'error',
'testing-library/no-wait-for-side-effects': 'error',
'testing-library/no-wait-for-snapshot': 'error',
Expand Down
100 changes: 0 additions & 100 deletions lib/rules/no-wait-for-empty-callback.ts

This file was deleted.

18 changes: 4 additions & 14 deletions lib/rules/prefer-find-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export const RULE_NAME = 'prefer-find-by';
export type MessageIds = 'preferFindBy';
type Options = [];

export const WAIT_METHODS = ['waitFor', 'waitForElement', 'wait'] as const;

export function getFindByQueryVariant(
queryMethod: string
): 'findAllBy' | 'findBy' {
Expand Down Expand Up @@ -63,7 +61,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
messages: {
preferFindBy:
'Prefer `{{queryVariant}}{{queryMethod}}` query over using `{{waitForMethodName}}` + `{{prevQuery}}`',
'Prefer `{{queryVariant}}{{queryMethod}}` query over using `waitFor` + `{{prevQuery}}`',
},
fixable: 'code',
schema: [],
Expand All @@ -75,12 +73,11 @@ export default createTestingLibraryRule<Options, MessageIds>({

/**
* Reports the invalid usage of wait* plus getBy/QueryBy methods and automatically fixes the scenario
* @param node - The CallExpresion node that contains the wait* method
* @param node - The CallExpression node that contains the waitFor method
* @param replacementParams - Object with info for error message and autofix:
* @param replacementParams.queryVariant - The variant method used to query: findBy/findAllBy.
* @param replacementParams.prevQuery - The query originally used inside `waitFor`
* @param replacementParams.queryMethod - Suffix string to build the query method (the query-part that comes after the "By"): LabelText, Placeholder, Text, Role, Title, etc.
* @param replacementParams.waitForMethodName - wait for method used: waitFor/wait/waitForElement
* @param replacementParams.fix - Function that applies the fix to correct the code
*/
function reportInvalidUsage(
Expand All @@ -89,20 +86,17 @@ export default createTestingLibraryRule<Options, MessageIds>({
queryVariant: 'findAllBy' | 'findBy';
queryMethod: string;
prevQuery: string;
waitForMethodName: string;
fix: TSESLint.ReportFixFunction;
}
) {
const { queryMethod, queryVariant, prevQuery, waitForMethodName, fix } =
replacementParams;
const { queryMethod, queryVariant, prevQuery, fix } = replacementParams;
context.report({
node,
messageId: 'preferFindBy',
data: {
queryVariant,
queryMethod,
prevQuery,
waitForMethodName,
},
fix,
});
Expand Down Expand Up @@ -336,7 +330,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
'AwaitExpression > CallExpression'(node: TSESTree.CallExpression) {
if (
!ASTUtils.isIdentifier(node.callee) ||
!helpers.isAsyncUtil(node.callee, WAIT_METHODS)
!helpers.isAsyncUtil(node.callee, ['waitFor'])
) {
return;
}
Expand All @@ -350,8 +344,6 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

const waitForMethodName = node.callee.name;

// ensure here it's one of the sync methods that we are calling
if (isScreenSyncQuery(argument)) {
const caller = getCaller(argument);
Expand Down Expand Up @@ -386,7 +378,6 @@ export default createTestingLibraryRule<Options, MessageIds>({
queryMethod,
queryVariant,
prevQuery: fullQueryMethod,
waitForMethodName,
fix(fixer) {
const property = (
(argument.body as TSESTree.CallExpression)
Expand Down Expand Up @@ -423,7 +414,6 @@ export default createTestingLibraryRule<Options, MessageIds>({
queryMethod,
queryVariant,
prevQuery: fullQueryMethod,
waitForMethodName,
fix(fixer) {
// we know from above callee is an Identifier
if (
Expand Down
8 changes: 1 addition & 7 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@ const ALL_QUERIES_COMBINATIONS = [
...ASYNC_QUERIES_COMBINATIONS,
];

const ASYNC_UTILS = [
'waitFor',
'waitForElementToBeRemoved',
'wait',
'waitForElement',
'waitForDomChange',
] as const;
const ASYNC_UTILS = ['waitFor', 'waitForElementToBeRemoved'] as const;

const DEBUG_UTILS = [
'debug',
Expand Down
2 changes: 1 addition & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { resolve } from 'path';

import plugin from '../lib';

const numberOfRules = 26;
const numberOfRules = 25;
const ruleNames = Object.keys(plugin.rules);

// eslint-disable-next-line jest/expect-expect
Expand Down
Loading