Skip to content

Commit fd9bc6b

Browse files
authored
docs: update README with more info around Aggressive Reporting (#339)
* docs: update README with more info around Aggressive Reporting * docs: remove unnecessary module.exports syntax * docs: improve running plugin on testing files * docs: remove wrong option for test files Co-authored-by: Michaël De Boey <[email protected]> * docs: transform bold texts into headings Co-authored-by: Michaël De Boey <[email protected]> Co-authored-by: Michaël De Boey <[email protected]> Closes #331
1 parent 19947ef commit fd9bc6b

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,39 @@ Then configure the rules you want to use within `rules` property of your `.eslin
7070
}
7171
```
7272

73+
### Run the plugin only against test files
74+
75+
With the default setup mentioned before, `eslint-plugin-testing-library` will be run against your whole codebase. If you want to run this plugin only against your tests files, you have the following options:
76+
77+
#### ESLint `overrides`
78+
79+
One way of restricting ESLint config by file patterns is by using [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-based-on-glob-patterns).
80+
81+
Assuming you are using the same pattern for your test files as [Jest by default](https://jestjs.io/docs/configuration#testmatch-arraystring), the following config would run `eslint-plugin-testing-library` only against your test files:
82+
83+
```json
84+
// .eslintrc
85+
{
86+
// 1) Here we have our usual config which applies to the whole project, so we don't put testing-library preset here.
87+
"extends": ["airbnb", "plugin:prettier/recommended"],
88+
89+
// 2) We load eslint-plugin-testing-library globally with other ESLint plugins.
90+
"plugins": ["react-hooks", "testing-library"],
91+
92+
"overrides": [
93+
{
94+
// 3) Now we enable eslint-plugin-testing-library rules or preset only for matching files!
95+
"files": ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
96+
"extends": ["plugin:testing-library/react"]
97+
},
98+
],
99+
};
100+
```
101+
102+
#### ESLint Cascading and Hierachy
103+
104+
Another approach for customizing ESLint config by paths is through [ESLint Cascading and Hierachy](https://eslint.org/docs/user-guide/configuring/configuration-files#cascading-and-hierarchy). This is useful if all your tests are placed under the same folder, so you can place there another `.eslintrc` where you enable `eslint-plugin-testing-library` for applying it only to the files under such folder, rather than enabling it on your global `.eslintrc` which would apply to your whole project.
105+
73106
## Shareable configurations
74107

75108
This plugin exports several recommended configurations that enforce good practices for specific Testing Library packages.
@@ -191,6 +224,12 @@ To enable this configuration use the `extends` property in your
191224
[react-badge]: https://img.shields.io/badge/-React-black?style=flat-square&logo=react&logoColor=white&labelColor=61DAFB&color=black
192225
[vue-badge]: https://img.shields.io/badge/-Vue-black?style=flat-square&logo=vue.js&logoColor=white&labelColor=4FC08D&color=black
193226

227+
## Aggressive Reporting
228+
229+
In v4 this plugin introduced a new feature called "Aggressive Reporting", which intends to detect Testing Library utils usages even if they don't come directly from a Testing Library package (i.e. [using a custom utility file to re-export everything from Testing Library](https://testing-library.com/docs/react-testing-library/setup/#custom-render)). You can [read more about this feature here](docs/migrating-to-v4-guide.md#aggressive-reporting).
230+
231+
If you are looking to restricting this feature, please refer to the [Shared Settings section](#shared-settings) to do so. It's not possible to switch this mechanism entirely off yet, but there will be a new option in the Shared Settings in the future to be able to achieve this.
232+
194233
## Shared Settings
195234

196235
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).
@@ -227,6 +266,27 @@ A list of function names that are valid as Testing Library custom renders. Relat
227266

228267
[You can find more details here](docs/migrating-to-v4-guide.md#testing-librarycustom-renders).
229268

269+
## Troubleshooting
270+
271+
### There are errors reported in non-testing files
272+
273+
If you find ESLint errors related to `eslint-plugin-testing-library` in files other than testing, this could be caused by [Aggressive Reporting](#aggressive-reporting).
274+
275+
You can avoid this by:
276+
277+
1. [running `eslint-plugin-testing-library` only against testing files](#run-the-plugin-only-against-test-files)
278+
2. [limiting the scope of Aggressive Reporting through Shared Settings](#shared-settings)
279+
280+
If you think the error you are getting is not related to this at all, please [fill a new issue](https://github.com/testing-library/eslint-plugin-testing-library/issues/new/choose) with as many details as possible.
281+
282+
### There are false positives in testing files
283+
284+
If you are getting false positive ESLint errors in your testing files, this could be caused by [Aggressive Reporting](#aggressive-reporting).
285+
286+
You can avoid this by [limiting the scope of Aggressive Reporting through Shared Settings](#shared-settings)
287+
288+
If you think the error you are getting is not related to this at all, please [fill a new issue](https://github.com/testing-library/eslint-plugin-testing-library/issues/new/choose) with as many details as possible.
289+
230290
## Contributors ✨
231291

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

0 commit comments

Comments
 (0)