Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: second round of bugfixes for v4 #314

Merged
merged 4 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | | |
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we can generate this table automatically in the future from rules properties :)

| [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)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes problem A

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',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes problem B

};

const angularRules = {
...domRules,
'testing-library/no-container': 'error',
'testing-library/no-debug': 'warn',
'testing-library/no-debug': 'error',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes problem C

'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