Skip to content

Commit cebdd21

Browse files
authored
docs: improve running plugin on testing files
1 parent 5413f14 commit cebdd21

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,35 @@ Then configure the rules you want to use within `rules` property of your `.eslin
7272

7373
### Run the plugin only against test files
7474

75-
With the default setup mentioned before `eslint-plugin-testing-library` will be run against your whole codebase. If want to run this plugin only against your tests files, you can do that by using [ESLint `overrides`](https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-based-on-glob-patterns).
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+
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).
7679

7780
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:
7881

79-
```javascript
82+
```json
8083
// .eslintrc
8184
{
82-
// here you have your usual config which applies to the whole project...
83-
extends: ['airbnb', 'plugin:prettier/recommended'],
84-
plugins: ['react-hooks'],
85+
// 1) Here we have our usual config which applies to the whole project, so we don't put testing-library preset here.
86+
"extends": ["airbnb", "plugin:prettier/recommended"],
8587

86-
overrides: [
88+
// 2) We load eslint-plugin-testing-library globally with other ESLint plugins.
89+
"plugins": ["react-hooks", "testing-library"],
90+
91+
"overrides": [
8792
{
88-
// ... and here you expand the ESLint plugins run for your tests files
89-
files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
90-
extends: ['plugin:testing-library/react'],
91-
plugins: ['testing-library'],
93+
// 3) Now we enable eslint-plugin-testing-library rules or preset only for matching files!
94+
"files": ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
95+
"extends": ["plugin:testing-library/react"]
9296
},
9397
],
9498
};
9599
```
96100

101+
**ESLint Cascading and Hierachy**
102+
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.
103+
97104
## Shareable configurations
98105

99106
This plugin exports several recommended configurations that enforce good practices for specific Testing Library packages.

0 commit comments

Comments
 (0)