forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-multiple-assertions-wait-for.ts
63 lines (57 loc) · 2.05 KB
/
no-multiple-assertions-wait-for.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'
import { getDocsUrl } from '../utils'
import { isBlockStatement, findClosestCallNode, isMemberExpression, isCallExpression, isIdentifier } from '../node-utils'
export const RULE_NAME = 'no-multiple-expect-wait-for';
const WAIT_EXPRESSION_QUERY =
'CallExpression[callee.name=/^(waitFor)$/]';
export type MessageIds = 'noMultipleAssertionWaitFor';
type Options = [];
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
"It's preferred to avoid multiple assertions in `waitFor`",
category: 'Best Practices',
recommended: false,
},
messages: {
noMultipleAssertionWaitFor: 'Avoid use multiple assertions to `waitFor`',
},
fixable: null,
schema: [],
},
defaultOptions: [],
create: function(context) {
function reporttMultipleAssertion(
node: TSESTree.BlockStatement
) {
const totalExpect = (body: Array<TSESTree.Node>): Array<TSESTree.Node> =>
body.filter((node: TSESTree.ExpressionStatement) => {
if (
isCallExpression(node.expression) &&
isMemberExpression(node.expression.callee) &&
isCallExpression(node.expression.callee.object)
) {
const object: TSESTree.CallExpression = node.expression.callee.object
const expressionName: string = isIdentifier(object.callee) && object.callee.name
return expressionName === 'expect'
} else {
return false
}
})
if (isBlockStatement(node) && totalExpect(node.body).length > 1) {
context.report({
node,
loc: node.loc.start,
messageId: 'noMultipleAssertionWaitFor',
});
}
}
return {
[`${WAIT_EXPRESSION_QUERY} > ArrowFunctionExpression > BlockStatement`]: reporttMultipleAssertion,
[`${WAIT_EXPRESSION_QUERY} > FunctionExpression > BlockStatement`]: reporttMultipleAssertion,
};
}
})