Skip to content

feat(prefer-explicit-assert): report on findBy* queries too #421

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

Merged
merged 6 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -204,7 +204,7 @@ To enable this configuration use the `extends` property in your
| [`testing-library/no-wait-for-multiple-assertions`](./docs/rules/no-wait-for-multiple-assertions.md) | Disallow the use of multiple `expect` calls inside `waitFor` | | |
| [`testing-library/no-wait-for-side-effects`](./docs/rules/no-wait-for-side-effects.md) | Disallow the use of side effects in `waitFor` | | |
| [`testing-library/no-wait-for-snapshot`](./docs/rules/no-wait-for-snapshot.md) | Ensures no snapshot is generated inside of a `waitFor` call | | |
| [`testing-library/prefer-explicit-assert`](./docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | |
| [`testing-library/prefer-explicit-assert`](./docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` and `findBy*` queries | | |
| [`testing-library/prefer-find-by`](./docs/rules/prefer-find-by.md) | Suggest using `find(All)By*` query instead of `waitFor` + `get(All)By*` to wait for elements | 🔧 | ![dom-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] |
| [`testing-library/prefer-presence-queries`](./docs/rules/prefer-presence-queries.md) | Ensure appropriate `get*`/`query*` queries are used with their respective matchers | | |
| [`testing-library/prefer-query-by-disappearance`](./docs/rules/prefer-query-by-disappearance.md) | Suggest using `queryBy*` queries when waiting for disappearance | | |
Expand Down
4 changes: 4 additions & 0 deletions lib/node-utils/is-node-of-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export const isReturnStatement = isNodeOfType(AST_NODE_TYPES.ReturnStatement);
export const isFunctionExpression = isNodeOfType(
AST_NODE_TYPES.FunctionExpression
);
export const isAwaitExpression = isNodeOfType(AST_NODE_TYPES.AwaitExpression);
export const isVariableDeclarator = isNodeOfType(
AST_NODE_TYPES.VariableDeclarator
);
70 changes: 67 additions & 3 deletions lib/rules/prefer-explicit-assert.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { TSESTree, ASTUtils } from '@typescript-eslint/experimental-utils';

import { createTestingLibraryRule } from '../create-testing-library-rule';
import { findClosestCallNode, isMemberExpression } from '../node-utils';
import {
findClosestCallNode,
isAwaitExpression,
isCallExpression,
isMemberExpression,
isVariableDeclarator,
} from '../node-utils';
import { PRESENCE_MATCHERS, ABSENCE_MATCHERS } from '../utils';

export const RULE_NAME = 'prefer-explicit-assert';
Expand All @@ -17,13 +23,49 @@ type Options = [
const isAtTopLevel = (node: TSESTree.Node) =>
!!node.parent?.parent && node.parent.parent.type === 'ExpressionStatement';

const isVariableDeclaration = (node: TSESTree.Node) => {
if (
isCallExpression(node.parent) &&
isAwaitExpression(node.parent.parent) &&
isVariableDeclarator(node.parent.parent.parent)
) {
return true; // const quxElement = await findByLabelText('qux')
}

if (
isCallExpression(node.parent) &&
isVariableDeclarator(node.parent.parent)
) {
return true; // const quxElement = findByLabelText('qux')
}

if (
isMemberExpression(node.parent) &&
isCallExpression(node.parent.parent) &&
isAwaitExpression(node.parent.parent.parent) &&
isVariableDeclarator(node.parent.parent.parent.parent)
) {
return true; // const quxElement = await screen.findByLabelText('qux')
}

if (
isMemberExpression(node.parent) &&
isCallExpression(node.parent.parent) &&
isVariableDeclarator(node.parent.parent.parent)
) {
return true; // const quxElement = screen.findByLabelText('qux')
}

return false;
};

export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'Suggest using explicit assertions rather than just `getBy*` queries',
'Suggest using explicit assertions rather than just `getBy*` and `findBy*` queries',
category: 'Best Practices',
recommendedConfig: {
dom: false,
Expand All @@ -34,7 +76,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
messages: {
preferExplicitAssert:
'Wrap stand-alone `getBy*` query with `expect` function for better explicit assertion',
'Wrap stand-alone `{{queryType}}` query with `expect` function for better explicit assertion',
preferExplicitAssertAssertion:
'`getBy*` queries must be asserted with `{{assertion}}`',
},
Expand All @@ -55,14 +97,33 @@ export default createTestingLibraryRule<Options, MessageIds>({
create(context, [options], helpers) {
const { assertion } = options;
const getQueryCalls: TSESTree.Identifier[] = [];
const findQueryCalls: TSESTree.Identifier[] = [];

return {
'CallExpression Identifier'(node: TSESTree.Identifier) {
if (helpers.isGetQueryVariant(node)) {
getQueryCalls.push(node);
}

if (helpers.isFindQueryVariant(node)) {
findQueryCalls.push(node);
}
},
'Program:exit'() {
findQueryCalls.forEach((queryCall) => {
if (isVariableDeclaration(queryCall)) {
return;
}

context.report({
node: queryCall,
messageId: 'preferExplicitAssert',
data: {
queryType: 'findBy*',
},
});
});

getQueryCalls.forEach((queryCall) => {
const node = isMemberExpression(queryCall.parent)
? queryCall.parent
Expand All @@ -72,6 +133,9 @@ export default createTestingLibraryRule<Options, MessageIds>({
context.report({
node: queryCall,
messageId: 'preferExplicitAssert',
data: {
queryType: 'getBy*',
},
});
}

Expand Down
92 changes: 92 additions & 0 deletions tests/lib/rules/prefer-explicit-assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ ruleTester.run(RULE_NAME, rule, {
...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
code: `const quxElement = get${queryMethod}('qux')`,
})),
...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
code: `
async () => {
const quxElement = await find${queryMethod}('qux')
}`,
})),
...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
code: `const quxElement = find${queryMethod}('qux')`,
})),
...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
code: `const quxElement = screen.find${queryMethod}('qux')`,
})),
...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
code: `
async () => {
const quxElement = await screen.find${queryMethod}('qux')
}`,
})),
...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
code: `() => { return get${queryMethod}('foo') }`,
})),
Expand Down Expand Up @@ -106,6 +124,65 @@ ruleTester.run(RULE_NAME, rule, {
errors: [
{
messageId: 'preferExplicitAssert',
data: {
queryType: 'getBy*',
},
},
],
} as const)
),
...COMBINED_QUERIES_METHODS.map(
(queryMethod) =>
({
code: `find${queryMethod}('foo')`,
errors: [
{
messageId: 'preferExplicitAssert',
data: { queryType: 'findBy*' },
},
],
} as const)
),
...COMBINED_QUERIES_METHODS.map(
(queryMethod) =>
({
code: `screen.find${queryMethod}('foo')`,
errors: [
{
messageId: 'preferExplicitAssert',
data: { queryType: 'findBy*' },
},
],
} as const)
),
...COMBINED_QUERIES_METHODS.map(
(queryMethod) =>
({
code: `
async () => {
await screen.find${queryMethod}('foo')
}
`,
errors: [
{
messageId: 'preferExplicitAssert',
data: { queryType: 'findBy*' },
},
],
} as const)
),
...COMBINED_QUERIES_METHODS.map(
(queryMethod) =>
({
code: `
async () => {
await find${queryMethod}('foo')
}
`,
errors: [
{
messageId: 'preferExplicitAssert',
data: { queryType: 'findBy*' },
},
],
} as const)
Expand All @@ -122,6 +199,9 @@ ruleTester.run(RULE_NAME, rule, {
messageId: 'preferExplicitAssert',
line: 3,
column: 15,
data: {
queryType: 'getBy*',
},
},
],
} as const)
Expand All @@ -135,6 +215,9 @@ ruleTester.run(RULE_NAME, rule, {
messageId: 'preferExplicitAssert',
line: 1,
column: 8,
data: {
queryType: 'getBy*',
},
},
],
} as const)
Expand All @@ -155,10 +238,16 @@ ruleTester.run(RULE_NAME, rule, {
{
messageId: 'preferExplicitAssert',
line: 3,
data: {
queryType: 'getBy*',
},
},
{
messageId: 'preferExplicitAssert',
line: 6,
data: {
queryType: 'getBy*',
},
},
],
} as const)
Expand All @@ -176,6 +265,9 @@ ruleTester.run(RULE_NAME, rule, {
errors: [
{
messageId: 'preferExplicitAssert',
data: {
queryType: 'getBy*',
},
},
],
} as const)
Expand Down