diff --git a/.changeset/healthy-melons-argue.md b/.changeset/healthy-melons-argue.md new file mode 100644 index 0000000..8b8f3e4 --- /dev/null +++ b/.changeset/healthy-melons-argue.md @@ -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. diff --git a/.changeset/quiet-fans-deny.md b/.changeset/quiet-fans-deny.md new file mode 100644 index 0000000..ee865bd --- /dev/null +++ b/.changeset/quiet-fans-deny.md @@ -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). diff --git a/packages/eslint-config-jest/README.md b/packages/eslint-config-jest/README.md index 4c3fa7b..10702fa 100644 --- a/packages/eslint-config-jest/README.md +++ b/packages/eslint-config-jest/README.md @@ -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 diff --git a/packages/eslint-config-jest/index.js b/packages/eslint-config-jest/index.js index a62c753..afcab45 100644 --- a/packages/eslint-config-jest/index.js +++ b/packages/eslint-config-jest/index.js @@ -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: [ @@ -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 @@ -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', + } + : {}), }, }, ]; diff --git a/packages/eslint-config-jest/package.json b/packages/eslint-config-jest/package.json index d68eb6a..7255efa 100644 --- a/packages/eslint-config-jest/package.json +++ b/packages/eslint-config-jest/package.json @@ -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", diff --git a/packages/eslint-config-react/README.md b/packages/eslint-config-react/README.md index 894e439..827307a 100644 --- a/packages/eslint-config-react/README.md +++ b/packages/eslint-config-react/README.md @@ -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 @@ -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 diff --git a/packages/eslint-config-react/package.json b/packages/eslint-config-react/package.json index 47ecf34..98c677f 100644 --- a/packages/eslint-config-react/package.json +++ b/packages/eslint-config-react/package.json @@ -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", @@ -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" diff --git a/packages/eslint-config-react/react-testing-library.js b/packages/eslint-config-react/react-testing-library.js deleted file mode 100644 index 3f091be..0000000 --- a/packages/eslint-config-react/react-testing-library.js +++ /dev/null @@ -1,22 +0,0 @@ -module.exports = { - plugins: ['testing-library'], - - overrides: [ - { - files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'], - - extends: [ - // https://github.com/testing-library/eslint-plugin-testing-library/blob/main/lib/configs/react.ts - 'plugin:testing-library/react', - ], - - rules: { - // 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', - }, - }, - ], -}; diff --git a/yarn.lock b/yarn.lock index 13a7eb8..2f34c0d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"