Skip to content

Commit 7b284b7

Browse files
committed
feat(eslint-config-react-app): Add jest, jest-dom & testing-library rules
1 parent d5c0fe2 commit 7b284b7

File tree

6 files changed

+71
-13
lines changed

6 files changed

+71
-13
lines changed

packages/eslint-config-react-app/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ If you want to use this ESLint configuration in a project not built with Create
1919
First, install this package, ESLint and the necessary plugins.
2020

2121
```sh
22-
npm install --save-dev eslint-config-react-app @typescript-eslint/eslint-plugin@^4.0.0 @typescript-eslint/parser@^4.0.0 babel-eslint@^10.0.0 eslint@^7.5.0 eslint-plugin-flowtype@^5.2.0 eslint-plugin-import@^2.22.0 eslint-plugin-jest@^24.0.0 eslint-plugin-jsx-a11y@^6.3.1 eslint-plugin-react@^7.20.3 eslint-plugin-react-hooks@^4.0.8
22+
npm install --save-dev eslint-config-react-app @typescript-eslint/eslint-plugin@^4.0.0 @typescript-eslint/parser@^4.0.0 babel-eslint@^10.0.0 eslint@^7.5.0 eslint-plugin-flowtype@^5.2.0 eslint-plugin-import@^2.22.0 eslint-plugin-jsx-a11y@^6.3.1 eslint-plugin-react@^7.20.3 eslint-plugin-react-hooks@^4.0.8
2323
```
2424

2525
Then create a file named `.eslintrc.json` with following contents in the root folder of your project:
@@ -39,7 +39,7 @@ This config also ships with optional Jest rules for ESLint (based on [`eslint-pl
3939
You'll first need to add the ESLint plugin for Jest (if you don't already have it installed).
4040

4141
```sh
42-
npm install --save-dev eslint-plugin-jest@^24.0.0
42+
npm install --save-dev eslint-plugin-jest@^24.0.0 eslint-plugin-jest-dom@^3.0.0 eslint-plugin-testing-library&^3.9.0
4343
```
4444

4545
You can then enable these rules by adding the Jest config to the `extends` array in your ESLint config.

packages/eslint-config-react-app/jest.js

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,62 @@
1212
// and we try not to use "ERROR" level at all.
1313

1414
module.exports = {
15-
plugins: ['jest'],
15+
plugins: ['jest', 'jest-dom', 'testing-library'],
1616
overrides: [
1717
{
1818
files: ['**/__tests__/**/*', '**/*.{spec,test}.*'],
1919
env: {
2020
'jest/globals': true,
2121
},
2222
// A subset of the recommended rules:
23-
// https://github.com/jest-community/eslint-plugin-jest#rules
2423
rules: {
25-
'jest/expect-expect': 'warn',
26-
'jest/no-identical-title': 'warn',
27-
'jest/valid-describe': 'warn',
28-
'jest/valid-expect': 'warn',
29-
'jest/valid-expect-in-promise': 'warn',
24+
// https://github.com/jest-community/eslint-plugin-jest
25+
'jest/expect-expect': 'off',
26+
'jest/no-commented-out-tests': 'warn',
27+
'jest/no-conditional-expect': 'error',
28+
'jest/no-deprecated-functions': 'warn',
29+
'jest/no-disabled-tests': 'warn',
30+
'jest/no-done-callback': 'error',
31+
'jest/no-export': 'error',
32+
'jest/no-focused-tests': 'warn',
33+
'jest/no-identical-title': 'error',
34+
'jest/no-interpolation-in-snapshots': 'error',
35+
'jest/no-jasmine-globals': 'error',
36+
'jest/no-jest-import': 'error',
37+
'jest/no-mocks-import': 'error',
38+
'jest/no-standalone-expect': 'off',
39+
'jest/no-test-callback': 'off',
40+
'jest/no-test-prefixes': 'warn',
41+
'jest/valid-describe': 'error',
42+
'jest/valid-expect': 'error',
43+
'jest/valid-expect-in-promise': 'error',
44+
'jest/valid-title': 'warn',
45+
46+
// https://github.com/testing-library/eslint-plugin-jest-dom
47+
'jest-dom/prefer-checked': 'warn',
48+
'jest-dom/prefer-empty': 'warn',
49+
'jest-dom/prefer-enabled-disabled': 'warn',
50+
'jest-dom/prefer-focus': 'warn',
51+
'jest-dom/prefer-required': 'warn',
52+
'jest-dom/prefer-to-have-attribute': 'warn',
53+
'jest-dom/prefer-to-have-style': 'warn',
54+
'jest-dom/prefer-to-have-text-content': 'warn',
55+
56+
// https://github.com/testing-library/eslint-plugin-testing-library
57+
'testing-library/await-async-query': 'error',
58+
'testing-library/await-async-utils': 'error',
59+
'testing-library/no-await-sync-query': 'warn',
60+
'testing-library/no-debug': 'warn',
61+
'testing-library/no-dom-import': ['error', 'react'],
62+
'testing-library/no-manual-cleanup': 'error',
63+
'testing-library/no-render-in-setup': 'error',
64+
'testing-library/no-wait-for-empty-callback': 'error',
65+
'testing-library/no-wait-for-snapshot': 'error',
66+
'testing-library/prefer-explicit-assert': 'warn',
67+
'testing-library/prefer-find-by': 'warn',
68+
'testing-library/prefer-presence-queries': 'warn',
69+
'testing-library/prefer-screen-queries': 'warn',
70+
'testing-library/prefer-wait-for': 'warn',
3071
},
3172
},
3273
],

packages/eslint-config-react-app/package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,22 @@
2424
"eslint-plugin-flowtype": "^5.2.0",
2525
"eslint-plugin-import": "^2.22.0",
2626
"eslint-plugin-jest": "^24.0.0",
27+
"eslint-plugin-jest-dom": "^3.0.0",
2728
"eslint-plugin-jsx-a11y": "^6.3.1",
2829
"eslint-plugin-react": "^7.20.3",
29-
"eslint-plugin-react-hooks": "^4.0.8"
30+
"eslint-plugin-react-hooks": "^4.0.8",
31+
"eslint-plugin-testing-library": "^3.9.0"
32+
},
33+
"peerDependenciesMeta": {
34+
"eslint-plugin-jest": {
35+
"optional": true
36+
},
37+
"eslint-plugin-jest-dom": {
38+
"optional": true
39+
},
40+
"eslint-plugin-testing-library": {
41+
"optional": true
42+
}
3043
},
3144
"dependencies": {
3245
"confusing-browser-globals": "^1.0.9"

packages/react-error-overlay/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "react-app",
2+
"extends": ["react-app", "react-app/jest"],
33
"rules": {
44
"curly": "warn"
55
}

packages/react-error-overlay/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@
4848
"eslint-config-react-app": "^5.2.1",
4949
"eslint-plugin-flowtype": "^5.2.0",
5050
"eslint-plugin-import": "^2.22.0",
51-
"eslint-plugin-jest": "^24.0.1",
51+
"eslint-plugin-jest": "^24.0.2",
52+
"eslint-plugin-jest-dom": "^3.2.3",
5253
"eslint-plugin-jsx-a11y": "^6.3.1",
5354
"eslint-plugin-react": "^7.20.6",
5455
"eslint-plugin-react-hooks": "^4.1.2",
56+
"eslint-plugin-testing-library": "^3.9.0",
5557
"flow-bin": "^0.116.0",
5658
"html-entities": "1.3.1",
5759
"jest": "26.4.2",

packages/react-scripts/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
"eslint-loader": "^4.0.2",
5050
"eslint-plugin-flowtype": "^5.2.0",
5151
"eslint-plugin-import": "^2.22.0",
52-
"eslint-plugin-jest": "^24.0.1",
52+
"eslint-plugin-jest": "^24.0.2",
53+
"eslint-plugin-jest-dom": "^3.2.3",
5354
"eslint-plugin-jsx-a11y": "^6.3.1",
5455
"eslint-plugin-react": "^7.20.6",
5556
"eslint-plugin-react-hooks": "^4.1.2",
57+
"eslint-plugin-testing-library": "^3.9.0",
5658
"file-loader": "6.1.0",
5759
"fs-extra": "^9.0.0",
5860
"html-webpack-plugin": "4.4.1",

0 commit comments

Comments
 (0)