Skip to content

Commit af32b0c

Browse files
author
Thomas Lombart
authored
refactor(prefer-explicit-assert): use new utils and remove custom query option (#274)
* refactor(prefer-explicit-assert): use new utils and remove custom query option * test: add custom query method
1 parent bd60704 commit af32b0c

File tree

3 files changed

+138
-152
lines changed

3 files changed

+138
-152
lines changed

docs/rules/prefer-explicit-assert.md

+2-14
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ expect(queryByText('foo')).toBeInTheDocument();
4343
await waitForElement(() => getByText('foo'));
4444
fireEvent.click(getByText('bar'));
4545
const quxElement = getByText('qux');
46-
47-
// call directly something different than Testing Library query
48-
getByNonTestingLibraryVariant('foo');
4946
```
5047

5148
## Options
5249

53-
This rule has a few options:
50+
This rule has one option:
5451

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

69-
- `customQueryNames`: this array option allows to extend default Testing
70-
Library queries with custom ones for including them into rule
71-
inspection.
72-
73-
```js
74-
"testing-library/prefer-explicit-assert": ["error", {"customQueryNames": ["getByIcon", "getBySomethingElse"]}],
75-
```
76-
7766
## When Not To Use It
7867

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

8270
## Further Reading
8371

lib/rules/prefer-explicit-assert.ts

+12-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
1-
import {
2-
ESLintUtils,
3-
TSESTree,
4-
ASTUtils,
5-
} from '@typescript-eslint/experimental-utils';
6-
import {
7-
getDocsUrl,
8-
ALL_QUERIES_METHODS,
9-
PRESENCE_MATCHERS,
10-
ABSENCE_MATCHERS,
11-
} from '../utils';
1+
import { TSESTree, ASTUtils } from '@typescript-eslint/experimental-utils';
2+
import { PRESENCE_MATCHERS, ABSENCE_MATCHERS } from '../utils';
123
import { findClosestCallNode, isMemberExpression } from '../node-utils';
134

5+
import { createTestingLibraryRule } from '../create-testing-library-rule';
6+
147
export const RULE_NAME = 'prefer-explicit-assert';
158
export type MessageIds =
169
| 'preferExplicitAssert'
1710
| 'preferExplicitAssertAssertion';
1811
type Options = [
1912
{
2013
assertion?: string;
21-
customQueryNames?: string[];
2214
}
2315
];
2416

25-
const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS.map(
26-
(queryMethod) => `get${queryMethod}`
27-
);
28-
29-
const isValidQuery = (node: TSESTree.Identifier, customQueryNames: string[]) =>
30-
ALL_GET_BY_QUERIES.includes(node.name) ||
31-
customQueryNames.includes(node.name);
32-
3317
const isAtTopLevel = (node: TSESTree.Node) =>
3418
node.parent.parent.type === 'ExpressionStatement';
3519

36-
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
20+
export default createTestingLibraryRule<Options, MessageIds>({
3721
name: RULE_NAME,
3822
meta: {
3923
type: 'suggestion',
@@ -59,26 +43,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5943
type: 'string',
6044
enum: PRESENCE_MATCHERS,
6145
},
62-
customQueryNames: {
63-
type: 'array',
64-
},
6546
},
6647
},
6748
],
6849
},
69-
defaultOptions: [
70-
{
71-
customQueryNames: [],
72-
},
73-
],
74-
75-
create: function (context, [options]) {
76-
const { customQueryNames, assertion } = options;
50+
defaultOptions: [{}],
51+
create(context, [options], helpers) {
52+
const { assertion } = options;
7753
const getQueryCalls: TSESTree.Identifier[] = [];
7854

7955
return {
8056
'CallExpression Identifier'(node: TSESTree.Identifier) {
81-
if (isValidQuery(node, customQueryNames)) {
57+
if (helpers.isGetByQuery(node)) {
8258
getQueryCalls.push(node);
8359
}
8460
},
@@ -93,7 +69,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
9369
node: queryCall,
9470
messageId: 'preferExplicitAssert',
9571
});
96-
} else if (assertion) {
72+
}
73+
74+
if (assertion) {
9775
const expectCallNode = findClosestCallNode(node, 'expect');
9876
if (!expectCallNode) return;
9977

0 commit comments

Comments
 (0)