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

refactor(prefer-explicit-assert): use new utils and remove custom query option #274

Merged
merged 2 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 2 additions & 14 deletions docs/rules/prefer-explicit-assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@ expect(queryByText('foo')).toBeInTheDocument();
await waitForElement(() => getByText('foo'));
fireEvent.click(getByText('bar'));
const quxElement = getByText('qux');

// call directly something different than Testing Library query
getByNonTestingLibraryVariant('foo');
```

## Options

This rule has a few options:
This rule has one option:

- `assertion`: this string allows defining the preferred assertion to use
with `getBy*` queries. By default, any assertion is valid (`toBeTruthy`,
Expand All @@ -66,18 +63,9 @@ This rule has a few options:
"testing-library/prefer-explicit-assert": ["error", {"assertion": "toBeInTheDocument"}],
```

- `customQueryNames`: this array option allows to extend default Testing
Library queries with custom ones for including them into rule
inspection.

```js
"testing-library/prefer-explicit-assert": ["error", {"customQueryNames": ["getByIcon", "getBySomethingElse"]}],
```

## When Not To Use It

If you prefer to use `getBy*` queries implicitly as an assert-like
method itself, then this rule is not recommended.
If you prefer to use `getBy*` queries implicitly as an assert-like method itself, then this rule is not recommended.

## Further Reading

Expand Down
46 changes: 12 additions & 34 deletions lib/rules/prefer-explicit-assert.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
import {
ESLintUtils,
TSESTree,
ASTUtils,
} from '@typescript-eslint/experimental-utils';
import {
getDocsUrl,
ALL_QUERIES_METHODS,
PRESENCE_MATCHERS,
ABSENCE_MATCHERS,
} from '../utils';
import { TSESTree, ASTUtils } from '@typescript-eslint/experimental-utils';
import { PRESENCE_MATCHERS, ABSENCE_MATCHERS } from '../utils';
import { findClosestCallNode, isMemberExpression } from '../node-utils';

import { createTestingLibraryRule } from '../create-testing-library-rule';

export const RULE_NAME = 'prefer-explicit-assert';
export type MessageIds =
| 'preferExplicitAssert'
| 'preferExplicitAssertAssertion';
type Options = [
{
assertion?: string;
customQueryNames?: string[];
}
];

const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS.map(
(queryMethod) => `get${queryMethod}`
);

const isValidQuery = (node: TSESTree.Identifier, customQueryNames: string[]) =>
ALL_GET_BY_QUERIES.includes(node.name) ||
customQueryNames.includes(node.name);

const isAtTopLevel = (node: TSESTree.Node) =>
node.parent.parent.type === 'ExpressionStatement';

export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
Expand All @@ -59,26 +43,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
type: 'string',
enum: PRESENCE_MATCHERS,
},
customQueryNames: {
type: 'array',
},
},
},
],
},
defaultOptions: [
{
customQueryNames: [],
},
],

create: function (context, [options]) {
const { customQueryNames, assertion } = options;
defaultOptions: [{}],
create(context, [options], helpers) {
const { assertion } = options;
const getQueryCalls: TSESTree.Identifier[] = [];

return {
'CallExpression Identifier'(node: TSESTree.Identifier) {
if (isValidQuery(node, customQueryNames)) {
if (helpers.isGetByQuery(node)) {
getQueryCalls.push(node);
}
},
Expand All @@ -93,7 +69,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
node: queryCall,
messageId: 'preferExplicitAssert',
});
} else if (assertion) {
}

if (assertion) {
const expectCallNode = findClosestCallNode(node, 'expect');
if (!expectCallNode) return;

Expand Down
Loading