Skip to content

Commit

Permalink
Move Testing Library rules to live in the Jest config (#39)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
melanieseltzer authored Sep 11, 2022
1 parent 08bfefb commit 02506cf
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 44 deletions.
7 changes: 7 additions & 0 deletions .changeset/healthy-melons-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@mels/eslint-config-jest': major
---

Move all Testing Library rules into this config.

`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.
5 changes: 5 additions & 0 deletions .changeset/quiet-fans-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mels/eslint-config-react': major
---

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).
6 changes: 5 additions & 1 deletion packages/eslint-config-jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

## ✨ Features

This package contains all my Jest rules as an extensible shared ESLint config.
This package contains all my Jest rules as an extensible shared ESLint config. It extends a number of configs as a base:

- [eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest) recommended rules
- [eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest) style rules
- [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)

## Install

Expand Down
31 changes: 30 additions & 1 deletion packages/eslint-config-jest/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
const readPkgUp = require('read-pkg-up');

const { packageJson } = readPkgUp.sync();

let hasReactTestingLibrary = false;

if (packageJson) {
const allDeps = Object.keys({
...packageJson.peerDependencies,
...packageJson.devDependencies,
...packageJson.dependencies,
});

hasReactTestingLibrary = allDeps.includes('@testing-library/react');
}

const overrides = [
{
files: [
Expand All @@ -12,7 +28,10 @@ const overrides = [

// https://github.com/jest-community/eslint-plugin-jest#rules
'plugin:jest/style',
],

// https://github.com/testing-library/eslint-plugin-testing-library/blob/main/lib/configs/react.ts
hasReactTestingLibrary ? 'plugin:testing-library/react' : null,
].filter(Boolean),

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

// https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/require-top-level-describe.md
'jest/require-top-level-describe': 'warn',

...(hasReactTestingLibrary
? {
// https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/prefer-explicit-assert.md
'testing-library/prefer-explicit-assert': 'warn',

// https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/prefer-wait-for.md
'testing-library/prefer-wait-for': 'error',
}
: {}),
},
},
];
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-config-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"lint": "eslint ."
},
"dependencies": {
"eslint-plugin-jest": "^26.8.7"
"eslint-plugin-jest": "^26.8.7",
"eslint-plugin-testing-library": "^5.6.0",
"read-pkg-up": "^7.0.1"
},
"peerDependencies": {
"@typescript-eslint/eslint-plugin": "^5.35.1",
Expand Down
12 changes: 0 additions & 12 deletions packages/eslint-config-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ This package contains all my React-related rules as an extensible shared ESLint
- React ([eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react))
- React Hooks ([eslint-plugin-react-hooks](https://github.com/facebook/react/tree/main/packages/eslint-plugin-react-hooks))
- JSX A11y (accessibility rules) ([eslint-plugin-jsx-a11y](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y))
- React Testing Library ([eslint-plugin-testing-library](https://github.com/testing-library/eslint-plugin-testing-library)) (if extended with `@mels/react/testing-library`)
- JSX runtime config (if extended with `@mels/react/jsx-runtime`)

## Install
Expand Down Expand Up @@ -55,17 +54,6 @@ module.exports = {
};
```

If you're using [React Testing Library](https://testing-library.com/), this package also exports a `testing-library` config:

```js
// .eslintrc.js

module.exports = {
extends: ['@mels/base', '@mels/react', '@mels/react/testing-library'],
// ... rest of config
};
```

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).

```js
Expand Down
5 changes: 1 addition & 4 deletions packages/eslint-config-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
"main": "./index.js",
"exports": {
".": "./index.js",
"./testing-library": "./react-testing-library.js",
"./jsx-runtime": "./jsx-runtime.js"
},
"files": [
"index.js",
"react-testing-library.js",
"jsx-runtime.js"
],
"license": "MIT",
Expand All @@ -29,8 +27,7 @@
"dependencies": {
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-react": "^7.31.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-testing-library": "^5.6.0"
"eslint-plugin-react-hooks": "^4.6.0"
},
"peerDependencies": {
"eslint": "^7.32.0 || ^8.0.0"
Expand Down
22 changes: 0 additions & 22 deletions packages/eslint-config-react/react-testing-library.js

This file was deleted.

6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4819,9 +4819,9 @@ eslint-plugin-testing-library@^5.0.1:
"@typescript-eslint/utils" "^5.13.0"

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

Expand Down

0 comments on commit 02506cf

Please sign in to comment.