Skip to content

Commit 02506cf

Browse files
Move Testing Library rules to live in the Jest config (#39)
* Move testing library rules into the jest config * Add changesets * Remove plugins array, testing-library/react already includes it * Fix the check * Fix the changeset * Fix readme
1 parent 08bfefb commit 02506cf

File tree

9 files changed

+54
-44
lines changed

9 files changed

+54
-44
lines changed

.changeset/healthy-melons-argue.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@mels/eslint-config-jest': major
3+
---
4+
5+
Move all Testing Library rules into this config.
6+
7+
`eslint-config-jest` will check under the hood if `@testing-library/react` is installed by the consumer, and if so, then it will automatically enable the Testing Library rules.

.changeset/quiet-fans-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mels/eslint-config-react': major
3+
---
4+
5+
Remove all Testing Library rules from this config. It now lives in [eslint-plugin-jest](https://github.com/melanieseltzer/toolkit/tree/main/packages/eslint-config-jest).

packages/eslint-config-jest/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
## ✨ Features
2424

25-
This package contains all my Jest rules as an extensible shared ESLint config.
25+
This package contains all my Jest rules as an extensible shared ESLint config. It extends a number of configs as a base:
26+
27+
- [eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest) recommended rules
28+
- [eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest) style rules
29+
- [eslint-plugin-testing-library](https://github.com/testing-library/eslint-plugin-testing-library/blob/main/lib/configs/react.ts) React rules (but only if this config detects that `@testing-library/react` is installed)
2630

2731
## Install
2832

packages/eslint-config-jest/index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
const readPkgUp = require('read-pkg-up');
2+
3+
const { packageJson } = readPkgUp.sync();
4+
5+
let hasReactTestingLibrary = false;
6+
7+
if (packageJson) {
8+
const allDeps = Object.keys({
9+
...packageJson.peerDependencies,
10+
...packageJson.devDependencies,
11+
...packageJson.dependencies,
12+
});
13+
14+
hasReactTestingLibrary = allDeps.includes('@testing-library/react');
15+
}
16+
117
const overrides = [
218
{
319
files: [
@@ -12,7 +28,10 @@ const overrides = [
1228

1329
// https://github.com/jest-community/eslint-plugin-jest#rules
1430
'plugin:jest/style',
15-
],
31+
32+
// https://github.com/testing-library/eslint-plugin-testing-library/blob/main/lib/configs/react.ts
33+
hasReactTestingLibrary ? 'plugin:testing-library/react' : null,
34+
].filter(Boolean),
1635

1736
rules: {
1837
// https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/consistent-test-it.md
@@ -50,6 +69,16 @@ const overrides = [
5069

5170
// https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/require-top-level-describe.md
5271
'jest/require-top-level-describe': 'warn',
72+
73+
...(hasReactTestingLibrary
74+
? {
75+
// https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/prefer-explicit-assert.md
76+
'testing-library/prefer-explicit-assert': 'warn',
77+
78+
// https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/prefer-wait-for.md
79+
'testing-library/prefer-wait-for': 'error',
80+
}
81+
: {}),
5382
},
5483
},
5584
];

packages/eslint-config-jest/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"lint": "eslint ."
2121
},
2222
"dependencies": {
23-
"eslint-plugin-jest": "^26.8.7"
23+
"eslint-plugin-jest": "^26.8.7",
24+
"eslint-plugin-testing-library": "^5.6.0",
25+
"read-pkg-up": "^7.0.1"
2426
},
2527
"peerDependencies": {
2628
"@typescript-eslint/eslint-plugin": "^5.35.1",

packages/eslint-config-react/README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ This package contains all my React-related rules as an extensible shared ESLint
2727
- React ([eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react))
2828
- React Hooks ([eslint-plugin-react-hooks](https://github.com/facebook/react/tree/main/packages/eslint-plugin-react-hooks))
2929
- JSX A11y (accessibility rules) ([eslint-plugin-jsx-a11y](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y))
30-
- React Testing Library ([eslint-plugin-testing-library](https://github.com/testing-library/eslint-plugin-testing-library)) (if extended with `@mels/react/testing-library`)
3130
- JSX runtime config (if extended with `@mels/react/jsx-runtime`)
3231

3332
## Install
@@ -55,17 +54,6 @@ module.exports = {
5554
};
5655
```
5756

58-
If you're using [React Testing Library](https://testing-library.com/), this package also exports a `testing-library` config:
59-
60-
```js
61-
// .eslintrc.js
62-
63-
module.exports = {
64-
extends: ['@mels/base', '@mels/react', '@mels/react/testing-library'],
65-
// ... rest of config
66-
};
67-
```
68-
6957
This package also exports a `jsx-runtime` config, if you're using the [new JSX transform from React 17](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#removing-unused-react-imports).
7058

7159
```js

packages/eslint-config-react/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
"main": "./index.js",
66
"exports": {
77
".": "./index.js",
8-
"./testing-library": "./react-testing-library.js",
98
"./jsx-runtime": "./jsx-runtime.js"
109
},
1110
"files": [
1211
"index.js",
13-
"react-testing-library.js",
1412
"jsx-runtime.js"
1513
],
1614
"license": "MIT",
@@ -29,8 +27,7 @@
2927
"dependencies": {
3028
"eslint-plugin-jsx-a11y": "^6.6.1",
3129
"eslint-plugin-react": "^7.31.1",
32-
"eslint-plugin-react-hooks": "^4.6.0",
33-
"eslint-plugin-testing-library": "^5.6.0"
30+
"eslint-plugin-react-hooks": "^4.6.0"
3431
},
3532
"peerDependencies": {
3633
"eslint": "^7.32.0 || ^8.0.0"

packages/eslint-config-react/react-testing-library.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,9 +4819,9 @@ eslint-plugin-testing-library@^5.0.1:
48194819
"@typescript-eslint/utils" "^5.13.0"
48204820

48214821
eslint-plugin-testing-library@^5.6.0:
4822-
version "5.6.0"
4823-
resolved "https://registry.yarnpkg.com/eslint-plugin-testing-library/-/eslint-plugin-testing-library-5.6.0.tgz#91e810ecb838f86decc9b5202876c87e42d73ea7"
4824-
integrity sha512-y63TRzPhGCMNsnUwMGJU1MFWc/3GvYw+nzobp9QiyNTTKsgAt5RKAOT1I34+XqVBpX1lC8bScoOjCkP7iRv0Mw==
4822+
version "5.6.3"
4823+
resolved "https://registry.yarnpkg.com/eslint-plugin-testing-library/-/eslint-plugin-testing-library-5.6.3.tgz#98878d3dbf4910f175d1ec0a3417ede147f3fc6e"
4824+
integrity sha512-//fhmCzopr8UDv5X2M3XMGxQ0j6KjKYZ+6PGqdV0woLiXTSTOAzuNsiTELGv883iCeUrYrnHhtObPXyiTMytVQ==
48254825
dependencies:
48264826
"@typescript-eslint/utils" "^5.13.0"
48274827

0 commit comments

Comments
 (0)