From ddb90073f5afa6b3b5c6799d97016f356ebc3559 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Mon, 9 Dec 2019 01:35:40 -0500 Subject: [PATCH 1/5] Use Jest DOM and Testing Library ESLint plugins --- packages/eslint-config-react-app/index.js | 2 ++ packages/eslint-config-react-app/package.json | 4 +++- packages/react-scripts/package.json | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/eslint-config-react-app/index.js b/packages/eslint-config-react-app/index.js index 0d29e7995a6..10db805b769 100644 --- a/packages/eslint-config-react-app/index.js +++ b/packages/eslint-config-react-app/index.js @@ -30,6 +30,8 @@ module.exports = { plugins: ['import', 'flowtype', 'jsx-a11y', 'react', 'react-hooks'], + extends: ['plugin:jest-dom/recommended', 'plugin:testing-library/react'], + env: { browser: true, commonjs: true, diff --git a/packages/eslint-config-react-app/package.json b/packages/eslint-config-react-app/package.json index 14c11fa0f6f..e0d6119f906 100644 --- a/packages/eslint-config-react-app/package.json +++ b/packages/eslint-config-react-app/package.json @@ -21,9 +21,11 @@ "eslint": "6.x", "eslint-plugin-flowtype": "3.x || 4.x", "eslint-plugin-import": "2.x", + "eslint-plugin-jest-dom": "1.x", "eslint-plugin-jsx-a11y": "6.x", "eslint-plugin-react": "7.x", - "eslint-plugin-react-hooks": "1.x || 2.x" + "eslint-plugin-react-hooks": "1.x || 2.x", + "eslint-plugin-testing-library": "1.x" }, "dependencies": { "confusing-browser-globals": "^1.0.9" diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index a5983f373c2..f93d811f0df 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -48,9 +48,11 @@ "eslint-loader": "3.0.3", "eslint-plugin-flowtype": "4.6.0", "eslint-plugin-import": "2.20.1", + "eslint-plugin-jest-dom": "1.3.0", "eslint-plugin-jsx-a11y": "6.2.3", "eslint-plugin-react": "7.19.0", "eslint-plugin-react-hooks": "^1.6.1", + "eslint-plugin-testing-library": "1.3.2", "file-loader": "4.3.0", "fs-extra": "^8.1.0", "html-webpack-plugin": "4.0.0-beta.11", From 7480e2db7ff969eb63e334157357f8cb00ecc33c Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Wed, 11 Dec 2019 14:53:09 -0500 Subject: [PATCH 2/5] Use Jest ESlint plugin --- packages/eslint-config-react-app/index.js | 6 +++++- packages/eslint-config-react-app/package.json | 1 + packages/react-scripts/package.json | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/eslint-config-react-app/index.js b/packages/eslint-config-react-app/index.js index 10db805b769..1deb79c168a 100644 --- a/packages/eslint-config-react-app/index.js +++ b/packages/eslint-config-react-app/index.js @@ -30,7 +30,11 @@ module.exports = { plugins: ['import', 'flowtype', 'jsx-a11y', 'react', 'react-hooks'], - extends: ['plugin:jest-dom/recommended', 'plugin:testing-library/react'], + extends: [ + 'plugin:jest/recommended', + 'plugin:jest-dom/recommended', + 'plugin:testing-library/react', + ], env: { browser: true, diff --git a/packages/eslint-config-react-app/package.json b/packages/eslint-config-react-app/package.json index e0d6119f906..19cbd586f09 100644 --- a/packages/eslint-config-react-app/package.json +++ b/packages/eslint-config-react-app/package.json @@ -21,6 +21,7 @@ "eslint": "6.x", "eslint-plugin-flowtype": "3.x || 4.x", "eslint-plugin-import": "2.x", + "eslint-plugin-jest": "23.x", "eslint-plugin-jest-dom": "1.x", "eslint-plugin-jsx-a11y": "6.x", "eslint-plugin-react": "7.x", diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index f93d811f0df..f5fee012b34 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -48,6 +48,7 @@ "eslint-loader": "3.0.3", "eslint-plugin-flowtype": "4.6.0", "eslint-plugin-import": "2.20.1", + "eslint-plugin-jest": "23.1.1", "eslint-plugin-jest-dom": "1.3.0", "eslint-plugin-jsx-a11y": "6.2.3", "eslint-plugin-react": "7.19.0", From 3679e2ff89d102a22b221cc41ba4bb52cd3cce4e Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Wed, 11 Dec 2019 18:19:17 -0500 Subject: [PATCH 3/5] Fix linter errors in react-error-overlay tests --- .../src/__tests__/extract-source-map.js | 13 +++-------- .../src/__tests__/get-source-map.js | 13 +++-------- .../src/__tests__/parser/generic.js | 22 +++++-------------- 3 files changed, 12 insertions(+), 36 deletions(-) diff --git a/packages/react-error-overlay/src/__tests__/extract-source-map.js b/packages/react-error-overlay/src/__tests__/extract-source-map.js index b47f5ab5f6d..43ca2c3e0fe 100644 --- a/packages/react-error-overlay/src/__tests__/extract-source-map.js +++ b/packages/react-error-overlay/src/__tests__/extract-source-map.js @@ -16,15 +16,8 @@ test('extracts last source map directive', async () => { }); test('errors when no source map', async () => { - expect.assertions(1); - const testFileName = 'test.js'; - try { - await extractSourceMapUrl( - testFileName, - `console.log('hi')\n\nconsole.log('bye')` - ); - } catch (e) { - expect(e).toBe(`Cannot find a source map directive for ${testFileName}.`); - } + await expect( + extractSourceMapUrl(testFileName, `console.log('hi')\n\nconsole.log('bye')`) + ).rejects.toBe(`Cannot find a source map directive for ${testFileName}.`); }); diff --git a/packages/react-error-overlay/src/__tests__/get-source-map.js b/packages/react-error-overlay/src/__tests__/get-source-map.js index 27c6d5f7893..89588691e8f 100644 --- a/packages/react-error-overlay/src/__tests__/get-source-map.js +++ b/packages/react-error-overlay/src/__tests__/get-source-map.js @@ -47,17 +47,10 @@ test('find an inline source map', async () => { }); test('error on a source map with unsupported encoding', async () => { - expect.assertions(2); - const file = fs .readFileSync(resolve(__dirname, '../../fixtures/junk-inline.mjs')) .toString('utf8'); - try { - await getSourceMap('/', file); - } catch (e) { - expect(e instanceof Error).toBe(true); - expect(e.message).toBe( - 'Sorry, non-base64 inline source-map encoding is not supported.' - ); - } + await expect(getSourceMap('/', file)).rejects.toThrow( + new Error('Sorry, non-base64 inline source-map encoding is not supported.') + ); }); diff --git a/packages/react-error-overlay/src/__tests__/parser/generic.js b/packages/react-error-overlay/src/__tests__/parser/generic.js index 32bade9311f..3cc11e3a544 100644 --- a/packages/react-error-overlay/src/__tests__/parser/generic.js +++ b/packages/react-error-overlay/src/__tests__/parser/generic.js @@ -8,23 +8,13 @@ import { parse } from '../../utils/parser'; test('throws on null', () => { - expect.assertions(2); - try { - parse(null); - } catch (e) { - expect(e instanceof Error).toBe(true); - expect(e.message).toBe('You cannot pass a null object.'); - } + expect(() => parse(null)).toThrow( + new Error('You cannot pass a null object.') + ); }); test('throws on unparsable', () => { - expect.assertions(2); - try { - parse({}); - } catch (e) { - expect(e instanceof Error).toBe(true); - expect(e.message).toBe( - 'The error you provided does not contain a stack trace.' - ); - } + expect(() => parse({})).toThrow( + new Error('The error you provided does not contain a stack trace.') + ); }); From bef07e09e8188e5782cd895aa3dc83f09ee1e2e4 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Wed, 11 Dec 2019 18:49:53 -0500 Subject: [PATCH 4/5] Only enable testing rules in test files --- packages/eslint-config-react-app/index.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/eslint-config-react-app/index.js b/packages/eslint-config-react-app/index.js index 1deb79c168a..59bef03bcc5 100644 --- a/packages/eslint-config-react-app/index.js +++ b/packages/eslint-config-react-app/index.js @@ -30,12 +30,6 @@ module.exports = { plugins: ['import', 'flowtype', 'jsx-a11y', 'react', 'react-hooks'], - extends: [ - 'plugin:jest/recommended', - 'plugin:jest-dom/recommended', - 'plugin:testing-library/react', - ], - env: { browser: true, commonjs: true, @@ -118,6 +112,17 @@ module.exports = { '@typescript-eslint/no-useless-constructor': 'warn', }, }, + { + files: [ + '**/__tests__/**/*.{js,jsx,ts,tsx}', + '**/*.{spec,test}.{js,jsx,ts,tsx}', + ], + extends: [ + 'plugin:jest/recommended', + 'plugin:jest-dom/recommended', + 'plugin:testing-library/react', + ], + }, ], // NOTE: When adding rules here, you need to make sure they are compatible with From 35664ef90b24cb3054f48e4856b8c6b5ca19cdbc Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Wed, 5 Feb 2020 18:41:54 -0500 Subject: [PATCH 5/5] Update new ESLint plugins --- packages/eslint-config-react-app/package.json | 4 ++-- packages/react-scripts/package.json | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/eslint-config-react-app/package.json b/packages/eslint-config-react-app/package.json index 19cbd586f09..ca6bfabedb1 100644 --- a/packages/eslint-config-react-app/package.json +++ b/packages/eslint-config-react-app/package.json @@ -22,11 +22,11 @@ "eslint-plugin-flowtype": "3.x || 4.x", "eslint-plugin-import": "2.x", "eslint-plugin-jest": "23.x", - "eslint-plugin-jest-dom": "1.x", + "eslint-plugin-jest-dom": "2.x", "eslint-plugin-jsx-a11y": "6.x", "eslint-plugin-react": "7.x", "eslint-plugin-react-hooks": "1.x || 2.x", - "eslint-plugin-testing-library": "1.x" + "eslint-plugin-testing-library": "2.x" }, "dependencies": { "confusing-browser-globals": "^1.0.9" diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index f5fee012b34..a451a0a9016 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -48,12 +48,12 @@ "eslint-loader": "3.0.3", "eslint-plugin-flowtype": "4.6.0", "eslint-plugin-import": "2.20.1", - "eslint-plugin-jest": "23.1.1", - "eslint-plugin-jest-dom": "1.3.0", + "eslint-plugin-jest": "23.6.0", + "eslint-plugin-jest-dom": "2.0.0", "eslint-plugin-jsx-a11y": "6.2.3", "eslint-plugin-react": "7.19.0", "eslint-plugin-react-hooks": "^1.6.1", - "eslint-plugin-testing-library": "1.3.2", + "eslint-plugin-testing-library": "2.2.1", "file-loader": "4.3.0", "fs-extra": "^8.1.0", "html-webpack-plugin": "4.0.0-beta.11",