Skip to content

Commit

Permalink
refactor(prefer-explicit-assert): use new utils and remove custom que…
Browse files Browse the repository at this point in the history
…ry option
  • Loading branch information
thomlom committed Dec 23, 2020
1 parent bd60704 commit 5929b94
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 149 deletions.
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

0 comments on commit 5929b94

Please sign in to comment.