Skip to content

v6.0.0 #796

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 24 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
20f23ad
feat(prefer-wait-for): remove rule (#648)
skovy Sep 19, 2022
9acba43
feat(no-render-in-setup): rename to `no-render-in-lifecycle` (#649)
skovy Sep 20, 2022
8f6ebf5
feat(no-manual-cleanup): add to React & Vue configs by default (#659)
skovy Oct 2, 2022
c76a7bf
feat(no-global-regexp-flag-in-query): add to all configs by default (…
skovy Oct 2, 2022
b4ce9bb
feat(await-fire-event): rename to `await-async-event` + add support f…
skovy Oct 2, 2022
b4b5394
feat(await-async-event): add basic fixer (#656)
skovy Oct 2, 2022
8dfe155
feat(no-node-access): add to DOM config by default (#661)
skovy Oct 2, 2022
8063f8f
feat(no-debugging-utils): warn instead of error in all configs (#662)
skovy Oct 3, 2022
e394ce0
feat(no-debugging-utils): enable all debug methods in all configs by …
skovy Oct 3, 2022
7ed3da8
feat(no-await-sync-query): rename to `no-await-sync-queries` (#666)
MichaelDeBoey Oct 5, 2022
afce5ea
feat(no-await-sync-events): add to DOM, Angular & React configs by de…
MichaelDeBoey Oct 5, 2022
c1803df
feat(await-async-query): rename to `await-async-queries` (#665)
MichaelDeBoey Oct 5, 2022
d0a5d35
docs: initial v6 migration guide (#668)
skovy Oct 5, 2022
7238f76
fix(await-async-event): pluralize to await-async-events (#670)
skovy Oct 5, 2022
9d5554c
fix(await-async-events): improve fixer (#675)
skovy Oct 14, 2022
83d45e5
Merge remote-tracking branch 'origin/main' into alpha
Belco90 Dec 20, 2022
e838a48
docs: regenerate after merge
Belco90 Dec 20, 2022
9316c3a
refactor: fix lint error
Belco90 Dec 20, 2022
c0b01b0
refactor: remove remaining support for old async utils (#706)
Belco90 Jan 5, 2023
b570537
Merge remote-tracking branch 'origin/main' into alpha
Belco90 Jan 11, 2023
640241d
Merge remote-tracking branch 'origin/main' into alpha
Belco90 Aug 5, 2023
8516c6c
refactor: adjust changes after merge
Belco90 Aug 5, 2023
acc2865
style: format files
Belco90 Aug 5, 2023
2a932fe
Merge branch 'main' into alpha
Belco90 Aug 10, 2023
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 .github/ISSUE_TEMPLATE/propose_new_rule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ body:
attributes:
label: Name for new rule
description: Suggest a name for the new rule that follows the [rule naming conventions](https://github.com/testing-library/eslint-plugin-testing-library/blob/main/CONTRIBUTING.md#rule-naming-conventions).
placeholder: prefer-wait-for
placeholder: prefer-find-by
validations:
required: true

Expand Down
69 changes: 35 additions & 34 deletions README.md

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions docs/migration-guides/v6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Guide: migrating to v6

If you are not on v5 yet, we recommend first following the [v5 migration guide](docs/migration-guides/v5.md).

## 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
- `await-async-query` is now called `await-async-queries`
- `no-await-async-query` is now called `no-await-async-queries`
- `no-render-in-setup` is now called `no-render-in-lifecycle`
- `no-await-sync-events` is now enabled by default in React, Angular, and DOM shared configs
- `no-manual-cleanup` is now enabled by default in React and Vue shared configs
- `no-global-regexp-flag-in-query` is now enabled by default in all shared configs
- `no-node-access` is now enabled by default in DOM shared config
- `no-debugging-utils` now reports all debugging utility methods by default
- `no-debugging-utils` now defaults to `warn` instead of `error` in all shared configs

## 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
- Renaming `testing-library/no-render-in-setup` to `testing-library/no-render-in-lifecycle` if you were referencing it manually somewhere
- Being aware of new rules enabled or changed above in shared configs which can lead to newly reported errors
149 changes: 149 additions & 0 deletions docs/rules/await-async-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Enforce promises from async event methods are handled (`testing-library/await-async-events`)

💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

Ensure that promises returned by `userEvent` (v14+) async methods or `fireEvent` (only Vue and Marko) async methods are handled properly.

## Rule Details

This rule aims to prevent users from forgetting to handle promise returned from async event
methods.

> ⚠️ `fireEvent` methods are async only on following Testing Library packages:
>
> - `@testing-library/vue` (supported by this plugin)
> - `@testing-library/svelte` (not supported yet by this plugin)
> - `@marko/testing-library` (supported by this plugin)

Examples of **incorrect** code for this rule:

```js
fireEvent.click(getByText('Click me'));

fireEvent.focus(getByLabelText('username'));
fireEvent.blur(getByLabelText('username'));

// wrap a fireEvent method within a function...
function triggerEvent() {
return fireEvent.click(button);
}
triggerEvent(); // ...but not handling promise from it is incorrect too
```

```js
userEvent.click(getByText('Click me'));
userEvent.tripleClick(getByText('Click me'));
userEvent.keyboard('foo');

// wrap a userEvent method within a function...
function triggerEvent() {
return userEvent.click(button);
}
triggerEvent(); // ...but not handling promise from it is incorrect too
```

Examples of **correct** code for this rule:

```js
// `await` operator is correct
await fireEvent.focus(getByLabelText('username'));
await fireEvent.blur(getByLabelText('username'));

// `then` method is correct
fireEvent.click(getByText('Click me')).then(() => {
// ...
});

// return the promise within a function is correct too!
const clickMeArrowFn = () => fireEvent.click(getByText('Click me'));

// wrap a fireEvent method within a function...
function triggerEvent() {
return fireEvent.click(button);
}
await triggerEvent(); // ...and handling promise from it is correct also

// using `Promise.all` or `Promise.allSettled` with an array of promises is valid
await Promise.all([
fireEvent.focus(getByLabelText('username')),
fireEvent.blur(getByLabelText('username')),
]);
```

```js
// `await` operator is correct
await userEvent.click(getByText('Click me'));
await userEvent.tripleClick(getByText('Click me'));

// `then` method is correct
userEvent.keyboard('foo').then(() => {
// ...
});

// return the promise within a function is correct too!
const clickMeArrowFn = () => userEvent.click(getByText('Click me'));

// wrap a userEvent method within a function...
function triggerEvent() {
return userEvent.click(button);
}
await triggerEvent(); // ...and handling promise from it is correct also

// using `Promise.all` or `Promise.allSettled` with an array of promises is valid
await Promise.all([
userEvent.click(getByText('Click me'));
userEvent.tripleClick(getByText('Click me'));
]);
```

## Options

- `eventModule`: `string` or `string[]`. Which event module should be linted for async event methods. Defaults to `userEvent` which should be used after v14. `fireEvent` should only be used with frameworks that have async fire event methods.

## Example

```json
{
"testing-library/await-async-events": [
2,
{
"eventModule": "userEvent"
}
]
}
```

```json
{
"testing-library/await-async-events": [
2,
{
"eventModule": "fireEvent"
}
]
}
```

```json
{
"testing-library/await-async-events": [
2,
{
"eventModule": ["fireEvent", "userEvent"]
}
]
}
```

## When Not To Use It

- `userEvent` is below v14, before all event methods are async
- `fireEvent` methods are sync for most Testing Library packages. If you are not using Testing Library package with async events, you shouldn't use this rule.

## Further Reading

- [Vue Testing Library fireEvent](https://testing-library.com/docs/vue-testing-library/api#fireevent)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Enforce promises from async queries to be handled (`testing-library/await-async-query`)
# Enforce promises from async queries to be handled (`testing-library/await-async-queries`)

💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.

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
84 changes: 0 additions & 84 deletions docs/rules/await-fire-event.md

This file was deleted.

5 changes: 3 additions & 2 deletions docs/rules/no-await-sync-events.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Disallow unnecessary `await` for sync events (`testing-library/no-await-sync-events`)

💼 This rule is enabled in the following configs: `angular`, `dom`, `react`.

<!-- end auto-generated rule header -->

Ensure that sync simulated events are not awaited unnecessarily.
Expand Down Expand Up @@ -107,5 +109,4 @@ module.exports = {
## Notes

- Since `user-event` v14 all its methods are async, so you should disable reporting them by setting the `eventModules` to just `"fire-event"` so `user-event` methods are not reported.
- There is another rule `await-fire-event`, which is only in Vue Testing
Library. Please do not confuse with this rule.
- There is another rule `await-async-events`, which is for awaiting async events for `user-event` v14 or `fire-event` only in Vue Testing Library. Please do not confuse with this rule.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Disallow unnecessary `await` for sync queries (`testing-library/no-await-sync-query`)
# Disallow unnecessary `await` for sync queries (`testing-library/no-await-sync-queries`)

💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.

Expand Down
4 changes: 2 additions & 2 deletions docs/rules/no-debugging-utils.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow the use of debugging utilities like `debug` (`testing-library/no-debugging-utils`)

💼 This rule is enabled in the following configs: `angular`, `marko`, `react`, `vue`.
⚠️ This rule _warns_ in the following configs: `angular`, `marko`, `react`, `vue`.

<!-- end auto-generated rule header -->

Expand All @@ -17,7 +17,7 @@ This rule supports disallowing the following debugging utilities:
- `logDOM`
- `prettyFormat`

By default, only `debug` and `logTestingPlaygroundURL` are disallowed.
By default, all are disallowed.

Examples of **incorrect** code for this rule:

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-global-regexp-flag-in-query.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Disallow the use of the global RegExp flag (/g) in queries (`testing-library/no-global-regexp-flag-in-query`)

💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-manual-cleanup.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Disallow the use of `cleanup` (`testing-library/no-manual-cleanup`)

💼 This rule is enabled in the following configs: `react`, `vue`.

<!-- end auto-generated rule header -->

`cleanup` is performed automatically if the testing framework you're using supports the `afterEach` global (like mocha, Jest, and Jasmine). In this case, it's unnecessary to do manual cleanups after each test unless you skip the auto-cleanup with environment variables such as `RTL_SKIP_AUTO_CLEANUP` for React.
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-node-access.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow direct Node access (`testing-library/no-node-access`)

💼 This rule is enabled in the following configs: `angular`, `marko`, `react`, `vue`.
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.

<!-- end auto-generated rule header -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Disallow the use of `render` in testing frameworks setup functions (`testing-library/no-render-in-setup`)
# Disallow the use of `render` in testing frameworks setup functions (`testing-library/no-render-in-lifecycle`)

💼 This rule is enabled in the following configs: `angular`, `marko`, `react`, `vue`.

Expand Down Expand Up @@ -87,5 +87,5 @@ it('Should have foo and bar', () => {
If you would like to allow the use of `render` (or a custom render function) in _either_ `beforeAll` or `beforeEach`, this can be configured using the option `allowTestingFrameworkSetupHook`. This may be useful if you have configured your tests to [skip auto cleanup](https://testing-library.com/docs/react-testing-library/setup#skipping-auto-cleanup). `allowTestingFrameworkSetupHook` is an enum that accepts either `"beforeAll"` or `"beforeEach"`.

```
"testing-library/no-render-in-setup": ["error", {"allowTestingFrameworkSetupHook": "beforeAll"}],
"testing-library/no-render-in-lifecycle": ["error", {"allowTestingFrameworkSetupHook": "beforeAll"}],
```
Loading