Skip to content

Commit

Permalink
fix: second round of bugfixes for v4 (#314)
Browse files Browse the repository at this point in the history
* fix: handle require without assignment properly

* fix: handle another require without assignment properly

* fix: update rule levels on presets

- remove prefer-user-event from presets
- report no-debug as error on presets

* docs: indicate prefer-user-event is not enabled in any preset

Closes #313
  • Loading branch information
Belco90 committed Apr 5, 2021
1 parent fd81fc5 commit 66df731
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][] | |
Expand Down
27 changes: 22 additions & 5 deletions lib/detect-testing-library-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isImportSpecifier,
isLiteral,
isMemberExpression,
isObjectPattern,
isProperty,
} from './node-utils';
import {
Expand Down Expand Up @@ -582,16 +583,22 @@ 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')
return requireNode.id;
}

// 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) &&
Expand All @@ -618,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;
Expand Down
7 changes: 3 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
10 changes: 3 additions & 7 deletions tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
}
Expand All @@ -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",
},
}
`;
Expand All @@ -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",
Expand All @@ -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",
},
}
Expand All @@ -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",
Expand All @@ -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",
},
}
Expand Down
12 changes: 12 additions & 0 deletions tests/create-testing-library-rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 66df731

Please sign in to comment.