From 755fc5460d511590fca40b24d2ec993bdddcfe91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Beltr=C3=A1n=20Alarc=C3=B3n?= Date: Mon, 5 Apr 2021 00:20:44 +0200 Subject: [PATCH 1/4] fix: handle require without assignment properly --- lib/detect-testing-library-utils.ts | 13 ++++++++++--- tests/create-testing-library-rule.test.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/detect-testing-library-utils.ts b/lib/detect-testing-library-utils.ts index 322398dc..37214b6a 100644 --- a/lib/detect-testing-library-utils.ts +++ b/lib/detect-testing-library-utils.ts @@ -17,6 +17,7 @@ import { isImportSpecifier, isLiteral, isMemberExpression, + isObjectPattern, isProperty, } from './node-utils'; import { @@ -582,7 +583,10 @@ export function detectTestingLibraryUtils< // it could be "import * as rtl from 'baz'" return node.specifiers.find((n) => isImportNamespaceSpecifier(n)); } else { - const requireNode = node.parent as TSESTree.VariableDeclarator; + if (!ASTUtils.isVariableDeclarator(node.parent)) { + return undefined; + } + const requireNode = node.parent; if (ASTUtils.isIdentifier(requireNode.id)) { // this is const rtl = require('foo') @@ -590,8 +594,11 @@ export function detectTestingLibraryUtils< } // this should be const { something } = require('foo') - const destructuring = requireNode.id as TSESTree.ObjectPattern; - const property = destructuring.properties.find( + if (!isObjectPattern(requireNode.id)) { + return undefined; + } + + const property = requireNode.id.properties.find( (n) => isProperty(n) && ASTUtils.isIdentifier(n.key) && diff --git a/tests/create-testing-library-rule.test.ts b/tests/create-testing-library-rule.test.ts index 575af623..93a67506 100644 --- a/tests/create-testing-library-rule.test.ts +++ b/tests/create-testing-library-rule.test.ts @@ -311,6 +311,18 @@ ruleTester.run(RULE_NAME, rule, { // Weird edge cases `(window as any).__THING = false;`, `thing.method.lastCall.args[0]();`, + + `// edge case when setting jest-dom up in jest config file - using require + require('@testing-library/jest-dom') + + foo() + `, + + `// edge case when setting jest-dom up in jest config file - using import + import '@testing-library/jest-dom' + + foo() + `, ], invalid: [ // Test Cases for Imports From d58563d00014cff1cc4c0452a5d94fd39127bd4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Beltr=C3=A1n=20Alarc=C3=B3n?= Date: Mon, 5 Apr 2021 00:51:35 +0200 Subject: [PATCH 2/4] fix: handle another require without assignment properly --- lib/detect-testing-library-utils.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/detect-testing-library-utils.ts b/lib/detect-testing-library-utils.ts index 37214b6a..33ca3350 100644 --- a/lib/detect-testing-library-utils.ts +++ b/lib/detect-testing-library-utils.ts @@ -625,8 +625,18 @@ export function detectTestingLibraryUtils< return userEventIdentifier.local; } } else { - const requireNode = importedUserEventLibraryNode.parent as TSESTree.VariableDeclarator; - return requireNode.id as TSESTree.Identifier; + if ( + !ASTUtils.isVariableDeclarator(importedUserEventLibraryNode.parent) + ) { + return null; + } + + const requireNode = importedUserEventLibraryNode.parent; + if (!ASTUtils.isIdentifier(requireNode.id)) { + return null; + } + + return requireNode.id; } return null; From eeab3b5b852e77918741aba191dfd5c721fcc34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Beltr=C3=A1n=20Alarc=C3=B3n?= Date: Mon, 5 Apr 2021 00:55:33 +0200 Subject: [PATCH 3/4] fix: update rule levels on presets - remove prefer-user-event from presets - report no-debug as error on presets --- lib/index.ts | 7 +++---- tests/__snapshots__/index.test.ts.snap | 10 +++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index f00fd7b1..4e874216 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -58,13 +58,12 @@ const domRules = { 'testing-library/no-wait-for-empty-callback': 'error', 'testing-library/prefer-find-by': 'error', 'testing-library/prefer-screen-queries': 'error', - 'testing-library/prefer-user-event': 'warn', }; const angularRules = { ...domRules, 'testing-library/no-container': 'error', - 'testing-library/no-debug': 'warn', + 'testing-library/no-debug': 'error', 'testing-library/no-dom-import': ['error', 'angular'], 'testing-library/no-node-access': 'error', 'testing-library/render-result-naming-convention': 'error', @@ -73,7 +72,7 @@ const angularRules = { const reactRules = { ...domRules, 'testing-library/no-container': 'error', - 'testing-library/no-debug': 'warn', + 'testing-library/no-debug': 'error', 'testing-library/no-dom-import': ['error', 'react'], 'testing-library/no-node-access': 'error', 'testing-library/render-result-naming-convention': 'error', @@ -83,7 +82,7 @@ const vueRules = { ...domRules, 'testing-library/await-fire-event': 'error', 'testing-library/no-container': 'error', - 'testing-library/no-debug': 'warn', + 'testing-library/no-debug': 'error', 'testing-library/no-dom-import': ['error', 'vue'], 'testing-library/no-node-access': 'error', 'testing-library/render-result-naming-convention': 'error', diff --git a/tests/__snapshots__/index.test.ts.snap b/tests/__snapshots__/index.test.ts.snap index 1f425687..1b3fa8c7 100644 --- a/tests/__snapshots__/index.test.ts.snap +++ b/tests/__snapshots__/index.test.ts.snap @@ -10,7 +10,7 @@ Object { "testing-library/await-async-utils": "error", "testing-library/no-await-sync-query": "error", "testing-library/no-container": "error", - "testing-library/no-debug": "warn", + "testing-library/no-debug": "error", "testing-library/no-dom-import": Array [ "error", "angular", @@ -20,7 +20,6 @@ Object { "testing-library/no-wait-for-empty-callback": "error", "testing-library/prefer-find-by": "error", "testing-library/prefer-screen-queries": "error", - "testing-library/prefer-user-event": "warn", "testing-library/render-result-naming-convention": "error", }, } @@ -39,7 +38,6 @@ Object { "testing-library/no-wait-for-empty-callback": "error", "testing-library/prefer-find-by": "error", "testing-library/prefer-screen-queries": "error", - "testing-library/prefer-user-event": "warn", }, } `; @@ -54,7 +52,7 @@ Object { "testing-library/await-async-utils": "error", "testing-library/no-await-sync-query": "error", "testing-library/no-container": "error", - "testing-library/no-debug": "warn", + "testing-library/no-debug": "error", "testing-library/no-dom-import": Array [ "error", "react", @@ -64,7 +62,6 @@ Object { "testing-library/no-wait-for-empty-callback": "error", "testing-library/prefer-find-by": "error", "testing-library/prefer-screen-queries": "error", - "testing-library/prefer-user-event": "warn", "testing-library/render-result-naming-convention": "error", }, } @@ -81,7 +78,7 @@ Object { "testing-library/await-fire-event": "error", "testing-library/no-await-sync-query": "error", "testing-library/no-container": "error", - "testing-library/no-debug": "warn", + "testing-library/no-debug": "error", "testing-library/no-dom-import": Array [ "error", "vue", @@ -91,7 +88,6 @@ Object { "testing-library/no-wait-for-empty-callback": "error", "testing-library/prefer-find-by": "error", "testing-library/prefer-screen-queries": "error", - "testing-library/prefer-user-event": "warn", "testing-library/render-result-naming-convention": "error", }, } From c96e9481ad665a426e1d1940c89e1b1190eb2380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Beltr=C3=A1n=20Alarc=C3=B3n?= Date: Mon, 5 Apr 2021 00:57:49 +0200 Subject: [PATCH 4/4] docs: indicate prefer-user-event is not enabled in any preset --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3261e7e9..61c95895 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ To enable this configuration use the `extends` property in your | [testing-library/prefer-explicit-assert](docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | | | [testing-library/prefer-find-by](docs/rules/prefer-find-by.md) | Suggest using `findBy*` methods instead of the `waitFor` + `getBy` queries | ![dom-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] | | [testing-library/prefer-presence-queries](docs/rules/prefer-presence-queries.md) | Enforce specific queries when checking element is present or not | | | -| [testing-library/prefer-user-event](docs/rules/prefer-user-event.md) | Suggest using `userEvent` library instead of `fireEvent` for simulating user interaction | ![dom-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | | +| [testing-library/prefer-user-event](docs/rules/prefer-user-event.md) | Suggest using `userEvent` library instead of `fireEvent` for simulating user interaction | | | | [testing-library/prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using screen while using queries | ![dom-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | | | [testing-library/prefer-wait-for](docs/rules/prefer-wait-for.md) | Use `waitFor` instead of deprecated wait methods | | ![fixable-badge][] | | [testing-library/render-result-naming-convention](docs/rules/render-result-naming-convention.md) | Enforce a valid naming for return value from `render` | ![angular-badge][] ![react-badge][] ![vue-badge][] | |