Skip to content

Commit 83d45e5

Browse files
committed
Merge remote-tracking branch 'origin/main' into alpha
# Conflicts: # README.md # docs/rules/await-fire-event.md # docs/rules/no-render-in-lifecycle.md # docs/rules/prefer-wait-for.md # lib/rules/await-fire-event.ts # tests/__snapshots__/index.test.ts.snap # tests/index.test.ts # tests/lib/rules/await-fire-event.test.ts
2 parents 9d5554c + 25f73c5 commit 83d45e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+566
-472
lines changed

.all-contributorsrc

+30
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,36 @@
544544
"code",
545545
"test"
546546
]
547+
},
548+
{
549+
"login": "NickBolles",
550+
"name": "Nick Bolles",
551+
"avatar_url": "https://avatars.githubusercontent.com/u/7891759?v=4",
552+
"profile": "https://nickbolles.com",
553+
"contributions": [
554+
"code",
555+
"test",
556+
"doc"
557+
]
558+
},
559+
{
560+
"login": "bmish",
561+
"name": "Bryan Mishkin",
562+
"avatar_url": "https://avatars.githubusercontent.com/u/698306?v=4",
563+
"profile": "http://www.linkedin.com/in/bmish",
564+
"contributions": [
565+
"doc",
566+
"tool"
567+
]
568+
},
569+
{
570+
"login": "theredspoon",
571+
"name": "Nim G",
572+
"avatar_url": "https://avatars.githubusercontent.com/u/20975696?v=4",
573+
"profile": "https://github.com/theredspoon",
574+
"contributions": [
575+
"doc"
576+
]
547577
}
548578
],
549579
"contributorsPerLine": 7,

.github/pull_request_template.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
## Checks
22

33
- [ ] I have read the [contributing guidelines](https://github.com/testing-library/eslint-plugin-testing-library/blob/main/CONTRIBUTING.md).
4-
- [ ] If some rule is added/updated/removed, I've regenerated the rules list (`npm run generate:rules-list`)
5-
- [ ] If some rule meta info is changed, I've regenerated the plugin shared configs (`npm run generate:configs`)
64

75
## Changes
86

.github/workflows/pipeline.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

2222
steps:
2323
- name: Cancel Previous Runs
24-
uses: styfle/cancel-workflow-action@0.10.1
24+
uses: styfle/cancel-workflow-action@0.11.0
2525

2626
- name: Checkout
2727
uses: actions/checkout@v3
@@ -45,6 +45,9 @@ jobs:
4545
- name: Check format
4646
run: npm run format:check
4747

48+
- name: Check autogenerated docs
49+
run: npm run docs:gen && npm run format && git diff --exit-code
50+
4851
tests:
4952
name: Tests (Node v${{ matrix.node }} - ESLint v${{ matrix.eslint }})
5053
runs-on: ubuntu-latest
@@ -56,7 +59,7 @@ jobs:
5659

5760
steps:
5861
- name: Cancel Previous Runs
59-
uses: styfle/cancel-workflow-action@0.10.1
62+
uses: styfle/cancel-workflow-action@0.11.0
6063

6164
- name: Checkout
6265
uses: actions/checkout@v3
@@ -88,7 +91,7 @@ jobs:
8891

8992
steps:
9093
- name: Cancel Previous Runs
91-
uses: styfle/cancel-workflow-action@0.10.1
94+
uses: styfle/cancel-workflow-action@0.11.0
9295

9396
- name: Checkout
9497
uses: actions/checkout@v3

.gitpod.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tasks:
2+
- init: npm install

README.md

+95-92
Large diffs are not rendered by default.

docs/rules/await-async-queries.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Enforce promises from async queries to be handled (`testing-library/await-async-queries`)
22

3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
6+
37
Ensure that promises returned by async queries are handled properly.
48

59
## Rule Details
@@ -19,6 +23,7 @@ problems in the tests. The promise will be considered as handled when:
1923
- wrapped within `Promise.all` or `Promise.allSettled` methods
2024
- chaining the `then` method
2125
- chaining `resolves` or `rejects` from jest
26+
- chaining `toResolve()` or `toReject()` from [jest-extended](https://github.com/jest-community/jest-extended#promise)
2227
- it's returned from a function (in this case, that particular function will be analyzed by this rule too)
2328

2429
Examples of **incorrect** code for this rule:
@@ -90,6 +95,12 @@ expect(findByTestId('alert')).resolves.toBe('Success');
9095
expect(findByTestId('alert')).rejects.toBe('Error');
9196
```
9297

98+
```js
99+
// using a toResolve/toReject matcher is also correct
100+
expect(findByTestId('alert')).toResolve();
101+
expect(findByTestId('alert')).toReject();
102+
```
103+
93104
```js
94105
// sync queries don't need to handle any promise
95106
const element = getByRole('role');

docs/rules/await-async-utils.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Enforce promises from async utils to be handled (`testing-library/await-async-utils`)
1+
# Enforce promises from async utils to be awaited properly (`testing-library/await-async-utils`)
2+
3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
26

37
Ensure that promises returned by async utils are handled properly.
48

@@ -20,6 +24,7 @@ problems in the tests. The promise will be considered as handled when:
2024
- wrapped within `Promise.all` or `Promise.allSettled` methods
2125
- chaining the `then` method
2226
- chaining `resolves` or `rejects` from jest
27+
- chaining `toResolve()` or `toReject()` from [jest-extended](https://github.com/jest-community/jest-extended#promise)
2328
- it's returned from a function (in this case, that particular function will be analyzed by this rule too)
2429

2530
Examples of **incorrect** code for this rule:
@@ -85,6 +90,12 @@ test('something correctly', async () => {
8590
waitFor(() => getByLabelText('email')),
8691
waitForElementToBeRemoved(() => document.querySelector('div.getOuttaHere')),
8792
]);
93+
94+
// Using jest resolves or rejects
95+
expect(waitFor(() => getByLabelText('email'))).resolves.toBeUndefined();
96+
97+
// Using jest-extended a toResolve/toReject matcher is also correct
98+
expect(waitFor(() => getByLabelText('email'))).toResolve();
8899
});
89100
```
90101

docs/rules/consistent-data-testid.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Enforces consistent naming for the data-testid attribute (`testing-library/consistent-data-testid`)
1+
# Ensures consistent usage of `data-testid` (`testing-library/consistent-data-testid`)
2+
3+
<!-- end auto-generated rule header -->
24

35
Ensure `data-testid` values match a provided regex. This rule is un-opinionated, and requires configuration.
46

docs/rules/no-await-sync-events.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Disallow unnecessary `await` for sync events (`testing-library/no-await-sync-events`)
22

3+
<!-- end auto-generated rule header -->
4+
35
Ensure that sync simulated events are not awaited unnecessarily.
46

57
## Rule Details

docs/rules/no-await-sync-queries.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Disallow unnecessary `await` for sync queries (`testing-library/no-await-sync-queries`)
22

3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
6+
37
Ensure that sync queries are not awaited unnecessarily.
48

59
## Rule Details

docs/rules/no-container.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Disallow the use of `container` methods (`testing-library/no-container`)
22

3+
💼 This rule is enabled in the following configs: `angular`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
6+
37
By using `container` methods like `.querySelector` you may lose a lot of the confidence that the user can really interact with your UI. Also, the test becomes harder to read, and it will break more frequently.
48

59
This applies to Testing Library frameworks built on top of **DOM Testing Library**

docs/rules/no-debugging-utils.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Disallow the use of debugging utilities (`testing-library/no-debugging-utils`)
1+
# Disallow the use of debugging utilities like `debug` (`testing-library/no-debugging-utils`)
2+
3+
💼 This rule is enabled in the following configs: `angular`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
26

37
Just like `console.log` statements pollutes the browser's output, debug statements also pollutes the tests if one of your teammates forgot to remove it. `debug` statements should be used when you actually want to debug your tests but should not be pushed to the codebase.
48

@@ -37,6 +41,8 @@ const { screen } = require('@testing-library/react');
3741
screen.debug();
3842
```
3943

44+
## Options
45+
4046
You can control which debugging utils are checked for with the `utilsToCheckFor` option:
4147

4248
```json

docs/rules/no-dom-import.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Disallow importing from DOM Testing Library (`testing-library/no-dom-import`)
22

3+
💼 This rule is enabled in the following configs: `angular`, `marko`, `react`, `vue`.
4+
5+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
6+
7+
<!-- end auto-generated rule header -->
8+
39
Ensure that there are no direct imports from `@testing-library/dom` or
410
`dom-testing-library` when using some testing library framework
511
wrapper.

docs/rules/no-global-regexp-flag-in-query.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Disallow the use of the global RegExp flag (/g) in queries (`testing-library/no-global-regexp-flag-in-query`)
22

3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
<!-- end auto-generated rule header -->
6+
37
Ensure that there are no global RegExp flags used when using queries.
48

59
## Rule Details

docs/rules/no-manual-cleanup.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Disallow the use of `cleanup` (`testing-library/no-manual-cleanup`)
22

3+
<!-- end auto-generated rule header -->
4+
35
`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.
46

57
## Rule Details

docs/rules/no-node-access.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Disallow direct Node access (`testing-library/no-node-access`)
22

3+
💼 This rule is enabled in the following configs: `angular`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
6+
37
The Testing Library already provides methods for querying DOM elements.
48

59
## Rule Details

docs/rules/no-promise-in-fire-event.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Disallow the use of promises passed to a `fireEvent` method (`testing-library/no-promise-in-fire-event`)
22

3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
6+
37
Methods from `fireEvent` expect to receive a DOM element. Passing a promise will end up in an error, so it must be prevented.
48

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

docs/rules/no-render-in-lifecycle.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
# Disallow the use of `render` in setup functions (`testing-library/no-render-in-lifecycle`)
1+
# Disallow the use of `render` in testing frameworks setup functions (`testing-library/no-render-in-lifecycle`)
2+
3+
💼 This rule is enabled in the following configs: `angular`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
26

37
## Rule Details
48

59
This rule disallows the usage of `render` (or a custom render function) in testing framework setup functions (`beforeEach` and `beforeAll`) in favor of moving `render` closer to test assertions.
610

11+
This rule reduces the amount of variable mutation, in particular avoiding nesting `beforeEach` functions. According to Kent C. Dodds, that results in vastly simpler test maintenance.
12+
13+
For more background on the origin and rationale for this best practice, read Kent C. Dodds's [Avoid Nesting when you're Testing](https://kentcdodds.com/blog/avoid-nesting-when-youre-testing).
14+
715
Examples of **incorrect** code for this rule:
816

917
```js
@@ -74,6 +82,8 @@ it('Should have foo and bar', () => {
7482
});
7583
```
7684

85+
## Options
86+
7787
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"`.
7888

7989
```

docs/rules/no-unnecessary-act.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Disallow wrapping Testing Library utils or empty callbacks in `act` (`testing-library/no-unnecessary-act`)
22

3+
💼 This rule is enabled in the following configs: `marko`, `react`.
4+
5+
<!-- end auto-generated rule header -->
6+
37
> ⚠️ The `act` method is only available on the following Testing Library packages:
48
>
59
> - `@testing-library/react` (supported by this plugin)

docs/rules/no-wait-for-empty-callback.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Empty callbacks inside `waitFor` and `waitForElementToBeRemoved` are not preferred (`testing-library/no-wait-for-empty-callback`)
1+
# Disallow empty callbacks for `waitFor` and `waitForElementToBeRemoved` (`testing-library/no-wait-for-empty-callback`)
2+
3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
26

37
## Rule Details
48

docs/rules/no-wait-for-multiple-assertions.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Disallow the use of multiple expect inside `waitFor` (`testing-library/no-wait-for-multiple-assertions`)
1+
# Disallow the use of multiple `expect` calls inside `waitFor` (`testing-library/no-wait-for-multiple-assertions`)
2+
3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
26

37
## Rule Details
48

docs/rules/no-wait-for-side-effects.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Disallow the use of side effects inside `waitFor` (`testing-library/no-wait-for-side-effects`)
1+
# Disallow the use of side effects in `waitFor` (`testing-library/no-wait-for-side-effects`)
2+
3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
26

37
## Rule Details
48

docs/rules/no-wait-for-snapshot.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Ensures no snapshot is generated inside a `waitFor` call (`testing-library/no-wait-for-snapshot`)
1+
# Ensures no snapshot is generated inside of a `waitFor` call (`testing-library/no-wait-for-snapshot`)
2+
3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
26

37
Ensure that no calls to `toMatchSnapshot` or `toMatchInlineSnapshot` are made from within a `waitFor` method (or any of the other async utility methods).
48

docs/rules/prefer-explicit-assert.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Suggest using explicit assertions rather than just `getBy*` queries (`testing-library/prefer-explicit-assert`)
1+
# Suggest using explicit assertions rather than standalone queries (`testing-library/prefer-explicit-assert`)
2+
3+
<!-- end auto-generated rule header -->
24

35
Testing Library `getBy*` queries throw an error if the element is not
46
found. Some users like this behavior to use the query itself as an

docs/rules/prefer-find-by.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
# Suggest using `findBy*` methods instead of the `waitFor` + `getBy` queries (`testing-library/prefer-find-by`)
1+
# Suggest using `find(All)By*` query instead of `waitFor` + `get(All)By*` to wait for elements (`testing-library/prefer-find-by`)
22

3-
findBy* queries are a simple combination of getBy* queries and waitFor. The findBy\* queries accept the waitFor options as the last argument. (i.e. screen.findByText('text', queryOptions, waitForOptions))
3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
6+
7+
<!-- end auto-generated rule header -->
8+
9+
`findBy*` queries are a simple combination of `getBy*` queries and `waitFor`. The `findBy*` queries accept the `waitFor` options as the last argument. (i.e. `screen.findByText('text', queryOptions, waitForOptions)`)
410

511
## Rule details
612

713
This rule aims to use `findBy*` or `findAllBy*` queries to wait for elements, rather than using `waitFor`, or the deprecated methods `waitForElement` and `wait`.
8-
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
14+
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.
915

1016
Examples of **incorrect** code for this rule
1117

@@ -78,7 +84,7 @@ await waitFor(() => expect(getAllByText('bar')).toBeDisabled());
7884

7985
## When Not To Use It
8086

81-
- Not encouraging use of findBy shortcut from testing library best practices
87+
- Not encouraging use of `findBy` shortcut from testing library best practices
8288

8389
## Further Reading
8490

docs/rules/prefer-presence-queries.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Enforce specific queries when checking element is present or not (`testing-library/prefer-presence-queries`)
1+
# Ensure appropriate `get*`/`query*` queries are used with their respective matchers (`testing-library/prefer-presence-queries`)
2+
3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
26

37
The (DOM) Testing Library allows to query DOM elements using different types of queries such as `get*` and `query*`. Using `get*` throws an error in case the element is not found, while `query*` returns null instead of throwing (or empty array for `queryAllBy*` ones). These differences are useful in some situations:
48

docs/rules/prefer-query-by-disappearance.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Suggest using `queryBy*` queries when waiting for disappearance (`testing-library/prefer-query-by-disappearance`)
22

3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
6+
37
## Rule Details
48

59
This rule enforces using `queryBy*` queries when waiting for disappearance with `waitForElementToBeRemoved`.

docs/rules/prefer-screen-queries.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Suggest using `screen` while querying (`testing-library/prefer-screen-queries`)
22

3+
💼 This rule is enabled in the following configs: `angular`, `dom`, `marko`, `react`, `vue`.
4+
5+
<!-- end auto-generated rule header -->
6+
37
## Rule Details
48

59
DOM Testing Library (and other Testing Library frameworks built on top of it) exports a `screen` object which has every query (and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain.

0 commit comments

Comments
 (0)