Skip to content

Commit 9365bf1

Browse files
authored
docs: update README for v4 (#317)
* docs: update README for v4 * docs: move detailed settings to migratuin guide * docs: format md files with prettier
1 parent f4b5958 commit 9365bf1

File tree

2 files changed

+140
-16
lines changed

2 files changed

+140
-16
lines changed

README.md

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,26 @@
3232

3333
You'll first need to install [ESLint](http://eslint.org):
3434

35-
```
36-
$ npm i eslint --save-dev
35+
```shell
36+
$ npm install --save-dev eslint
37+
# or
38+
$ yarn add --dev eslint
3739
```
3840

3941
Next, install `eslint-plugin-testing-library`:
4042

41-
```
42-
$ npm install eslint-plugin-testing-library --save-dev
43+
```shell
44+
$ npm install --save-dev eslint-plugin-testing-library
45+
# or
46+
$ yarn add --dev eslint-plugin-testing-library
4347
```
4448

4549
**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-testing-library` globally.
4650

51+
## Migrating to v4
52+
53+
You can find [here a detailed guide for migrating `eslint-plugin-testing-library` to v4](docs/migrating-to-v4-guide.md).
54+
4755
## Usage
4856

4957
Add `testing-library` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
@@ -54,22 +62,37 @@ Add `testing-library` to the plugins section of your `.eslintrc` configuration f
5462
}
5563
```
5664

57-
Then configure the rules you want to use under the rules section.
65+
Then configure the rules you want to use within `rules` property of your `.eslintrc`:
5866

5967
```json
6068
{
6169
"rules": {
6270
"testing-library/await-async-query": "error",
6371
"testing-library/no-await-sync-query": "error",
64-
"testing-library/no-debug": "warn"
72+
"testing-library/no-debug": "warn",
73+
"testing-library/no-dom-import": "off"
6574
}
6675
}
6776
```
6877

6978
## Shareable configurations
7079

7180
This plugin exports several recommended configurations that enforce good practices for specific Testing Library packages.
72-
You can find more info about enabled rules in the [Supported Rules section](#supported-rules) within the `Configurations` column.
81+
You can find more info about enabled rules in the [Supported Rules section](#supported-rules), under the `Configurations` column.
82+
83+
Since each one of these configurations is aimed at a particular Testing Library package, they are not extendable between them, so you should use only one of them at once per `eslintrc` file. For example, if you want to enable recommended configuration for React, you don't need to combine it somehow with DOM one:
84+
85+
```json
86+
// ❌ Don't do this
87+
{
88+
"extends": ["plugin:testing-library/dom", "plugin:testing-library/react"]
89+
}
90+
91+
// ✅ Do just this instead
92+
{
93+
"extends": ["plugin:testing-library/react"]
94+
}
95+
```
7396

7497
### DOM Testing Library
7598

@@ -172,6 +195,42 @@ To enable this configuration use the `extends` property in your
172195
[react-badge]: https://img.shields.io/badge/-React-black?style=flat-square&logo=react&logoColor=white&labelColor=61DAFB&color=black
173196
[vue-badge]: https://img.shields.io/badge/-Vue-black?style=flat-square&logo=vue.js&logoColor=white&labelColor=4FC08D&color=black
174197

198+
## Shared Settings
199+
200+
There are some configuration options available that will be shared across all the plugin rules. This is achieved using [ESLint Shared Settings](https://eslint.org/docs/user-guide/configuring/configuration-files#adding-shared-settings). These Shared Settings are meant to be used if you need to restrict the Aggressive Reporting mechanism, which is an out of the box advanced feature to lint Testing Library usages in a simpler way for most of the users. **So please before configuring any of these settings**, read more about [the advantages of `eslint-plugin-testing-library` Aggressive Reporting mechanism](docs/migrating-to-v4-guide.md#aggressive-reporting), and [how it's affected by these settings](docs/migrating-to-v4-guide.md#shared-settings).
201+
202+
If you are sure about configuring the settings, these are the options available:
203+
204+
### `testing-library/utils-module`
205+
206+
The name of your custom utility file from where you re-export everything from Testing Library package.
207+
208+
```json
209+
// .eslintrc
210+
{
211+
"settings": {
212+
"testing-library/utils-module": "my-custom-test-utility-file"
213+
}
214+
}
215+
```
216+
217+
[You can find more details here](docs/migrating-to-v4-guide.md#testing-libraryutils-module).
218+
219+
### `testing-library/custom-renders`
220+
221+
A list of function names that are valid as Testing Library custom renders. Relates to [Aggressive Reporting - Renders](docs/migrating-to-v4-guide.md#renders)
222+
223+
```json
224+
// .eslintrc
225+
{
226+
"settings": {
227+
"testing-library/custom-renders": ["display", "renderWithProviders"]
228+
}
229+
}
230+
```
231+
232+
[You can find more details here](docs/migrating-to-v4-guide.md#testing-librarycustom-renders).
233+
175234
## Contributors ✨
176235

177236
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

docs/migrating-to-v4-guide.md

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ _You can find the motivation behind this behavior on [this issue comment](https:
153153

154154
By default, `eslint-plugin-testing-library` v4 won't check from which module are the utils imported. This means it doesn't matter if you are importing the utils from `@testing-library/*`, `test-utils` or `whatever`.
155155

156-
There is a new Shared Setting to restrict this scope though: [`utils-module`](#utils-module). By using this setting, only utils imported from `@testing-library/*` packages, or the custom one indicated in this setting would be reported.
156+
There is a new Shared Setting to restrict this scope though: [`utils-module`](#testing-libraryutils-module). By using this setting, only utils imported from `@testing-library/*` packages, or the custom one indicated in this setting would be reported.
157157

158158
### Renders
159159

160160
By default, `eslint-plugin-testing-library` v4 will assume that all methods which names contain "render" should be reported. This means it doesn't matter if you are rendering your elements for testing using `render`, `customRender` or `renderWithRedux`.
161161

162-
There is a new Shared Setting to restrict this scope though: [`custom-renders`](#custom-renders). By using this setting, only methods strictly named `render` or as one of the indicated Custom Renders would be reported.
162+
There is a new Shared Setting to restrict this scope though: [`custom-renders`](#testing-librarycustom-renders). By using this setting, only methods strictly named `render` or as one of the indicated Custom Renders would be reported.
163163

164164
### Queries
165165

@@ -175,24 +175,54 @@ To avoid collision with settings from other ESLint plugins, all the properties f
175175

176176
⚠️ **Please be aware of using these settings will disable part of [Aggressive Reporting](#aggressive-reporting).**
177177

178-
### `utils-module`
178+
### `testing-library/utils-module`
179179

180-
Relates to [Aggressive Reporting - Imports](#imports). This setting (just a string) allows you to indicate which is the only Custom Module you'd like to be reported by `eslint-plugin-testing-library`.
180+
The name of your custom utility file from where you re-export everything from Testing Library package. Relates to [Aggressive Reporting - Imports](#imports).
181181

182182
```json
183183
// .eslintrc
184184
{
185185
"settings": {
186-
"testing-library/utils-module": "my-custom-test-utils"
186+
"testing-library/utils-module": "my-custom-test-utility-file"
187187
}
188188
}
189189
```
190190

191-
The previous setting example would force `eslint-plugin-testing-library` to only report Testing Library utils coming from `@testing-library/*` package or `my-custom-test-utils`, silencing potential errors for utils imported from somewhere else.
191+
Enabling this setting, you'll restrict the errors reported by the plugin to only those utils being imported from this custom utility file, or some `@testing-library/*` package. The previous setting example would cause:
192192

193-
### `custom-renders`
193+
```javascript
194+
import { waitFor } from '@testing-library/react';
195+
196+
test('testing-library/utils-module setting example', () => {
197+
// ✅ this would be reported since this invalid usage of an util
198+
// is imported from `@testing-library/*` package
199+
waitFor(/* some invalid usage to be reported */);
200+
});
201+
```
202+
203+
```javascript
204+
import { waitFor } from '../my-custom-test-utility-file';
205+
206+
test('testing-library/utils-module setting example', () => {
207+
// ✅ this would be reported since this invalid usage of an util
208+
// is imported from specified custom utility file.
209+
waitFor(/* some invalid usage to be reported */);
210+
});
211+
```
194212

195-
Relates to [Aggressive Reporting - Renders](#renders). This setting (array of strings) allows you to indicate which are the only Custom Renders you'd like to be reported by `eslint-plugin-testing-library`.
213+
```javascript
214+
import { waitFor } from '../somewhere-else';
215+
216+
test('testing-library/utils-module setting example', () => {
217+
// ❌ this would NOT be reported since this invalid usage of an util
218+
// is NOT imported from either `@testing-library/*` package or specified custom utility file.
219+
waitFor(/* some invalid usage to be reported */);
220+
});
221+
```
222+
223+
### `testing-library/custom-renders`
224+
225+
A list of function names that are valid as Testing Library custom renders. Relates to [Aggressive Reporting - Renders](#renders)
196226

197227
```json
198228
// .eslintrc
@@ -203,4 +233,39 @@ Relates to [Aggressive Reporting - Renders](#renders). This setting (array of st
203233
}
204234
```
205235

206-
The previous setting example would force `eslint-plugin-testing-library` to only report Testing Library renders named `render` (built-in one is always included), `display` and `renderWithProviders`, silencing potential errors for others custom renders.
236+
Enabling this setting, you'll restrict the errors reported by the plugin related to `render` somehow to only those functions sharing a name with one of the elements of that list, or built-in `render`. The previous setting example would cause:
237+
238+
```javascript
239+
import {
240+
render,
241+
display,
242+
renderWithProviders,
243+
renderWithRedux,
244+
} from 'test-utils';
245+
import Component from 'somewhere';
246+
247+
const setupA = () => renderWithProviders(<Component />);
248+
const setupB = () => renderWithRedux(<Component />);
249+
250+
test('testing-library/custom-renders setting example', () => {
251+
// ✅ this would be reported since `render` is a built-in Testing Library util
252+
const invalidUsage = render(<Component />);
253+
254+
// ✅ this would be reported since `display` has been set as `custom-render`
255+
const invalidUsage = display(<Component />);
256+
257+
// ✅ this would be reported since `renderWithProviders` has been set as `custom-render`
258+
const invalidUsage = renderWithProviders(<Component />);
259+
260+
// ❌ this would NOT be reported since `renderWithRedux` isn't a `custom-render` or built-in one
261+
const invalidUsage = renderWithRedux(<Component />);
262+
263+
// ✅ this would be reported since it wraps `renderWithProviders`,
264+
// which has been set as `custom-render`
265+
const invalidUsage = setupA(<Component />);
266+
267+
// ❌ this would NOT be reported since it wraps `renderWithRedux`,
268+
// which isn't a `custom-render` or built-in one
269+
const invalidUsage = setupB(<Component />);
270+
});
271+
```

0 commit comments

Comments
 (0)