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:
ESLint overrides
One way of restricting ESLint config by file patterns is by using ESLint overrides.
Assuming you are using the same pattern for your test files as Jest by default, the following config would run eslint-plugin-testing-library only against your test files:
// .eslintrc{// 1) Here we have our usual config which applies to the whole project, so we don't put testing-library preset here.extends: ['airbnb','plugin:prettier/recommended'],// 2) We load other plugins than eslint-plugin-testing-library globally if we want to.plugins: ['react-hooks'],overrides: [{// 3) Now we enable eslint-plugin-testing-library rules or preset only for matching testing files!files: ['**/__tests__/**/*.[jt]s?(x)','**/?(*.)+(spec|test).[jt]s?(x)'],extends: ['plugin:testing-library/react'],},],}
ESLint Cascading and Hierarchy
Another approach for customizing ESLint config by paths is through ESLint 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.
Shareable configurations
This plugin exports several recommended configurations that enforce good practices for specific Testing Library packages.
You can find more info about enabled rules in the Supported Rules section, under the Configurations column.
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:
// ❌ Don't do this{extends: ['plugin:testing-library/dom','plugin:testing-library/react'],}
// ✅ Just do this instead{extends: ['plugin:testing-library/react'],}
DOM Testing Library
Enforces recommended rules for DOM Testing Library.
To enable this configuration use the extends property in your
.eslintrc config file:
{
"extends": ["plugin:testing-library/dom"]
}
Angular
Enforces recommended rules for Angular Testing Library.
To enable this configuration use the extends property in your
.eslintrc config file:
{
"extends": ["plugin:testing-library/angular"]
}
React
Enforces recommended rules for React Testing Library.
To enable this configuration use the extends property in your
.eslintrc config file:
{
"extends": ["plugin:testing-library/react"]
}
Vue
Enforces recommended rules for Vue Testing Library.
To enable this configuration use the extends property in your
.eslintrc config file:
{
"extends": ["plugin:testing-library/vue"]
}
Marko
Enforces recommended rules for Marko Testing Library.
To enable this configuration use the extends property in your
.eslintrc config file:
{
"extends": ["plugin:testing-library/marko"]
}
Supported Rules
Remember that all rules from this plugin are prefixed by "testing-library/"
请发表评论