forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefer-implicit-assert.ts
137 lines (128 loc) · 3.74 KB
/
prefer-implicit-assert.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
TSESTree,
ASTUtils,
AST_NODE_TYPES,
TSESLint,
} from '@typescript-eslint/utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import { TestingLibrarySettings } from '../create-testing-library-rule/detect-testing-library-utils';
import { isCallExpression, isMemberExpression } from '../node-utils';
export const RULE_NAME = 'prefer-implicit-assert';
export type MessageIds = 'preferImplicitAssert';
type Options = [];
const isCalledUsingSomeObject = (node: TSESTree.Identifier) =>
isMemberExpression(node.parent) &&
node.parent.object.type === AST_NODE_TYPES.Identifier;
const isCalledInExpect = (
node: TSESTree.Identifier | TSESTree.Node,
isAsyncQuery: boolean
) => {
if (isAsyncQuery) {
return (
isCallExpression(node.parent) &&
ASTUtils.isAwaitExpression(node.parent.parent) &&
isCallExpression(node.parent.parent.parent) &&
ASTUtils.isIdentifier(node.parent.parent.parent.callee) &&
node.parent.parent.parent.callee.name === 'expect'
);
}
return (
isCallExpression(node.parent) &&
isCallExpression(node.parent.parent) &&
ASTUtils.isIdentifier(node.parent.parent.callee) &&
node.parent.parent.callee.name === 'expect'
);
};
const reportError = (
context: Readonly<
TSESLint.RuleContext<'preferImplicitAssert', []> & {
settings: TestingLibrarySettings;
}
>,
node: TSESTree.Identifier | TSESTree.Node | undefined,
queryType: string
) => {
if (node) {
return context.report({
node,
messageId: 'preferImplicitAssert',
data: {
queryType,
},
});
}
};
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description:
'Suggest using implicit assertions for getBy* & findBy* queries',
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
svelte: false,
marko: false,
},
},
messages: {
preferImplicitAssert:
"Don't wrap `{{queryType}}` query with `expect` & presence matchers like `toBeInTheDocument` or `not.toBeNull` as `{{queryType}}` queries fail implicitly when element is not found",
},
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
const findQueryCalls: TSESTree.Identifier[] = [];
const getQueryCalls: TSESTree.Identifier[] = [];
return {
'CallExpression Identifier'(node: TSESTree.Identifier) {
if (helpers.isFindQueryVariant(node)) {
findQueryCalls.push(node);
}
if (helpers.isGetQueryVariant(node)) {
getQueryCalls.push(node);
}
},
'Program:exit'() {
findQueryCalls.forEach((queryCall) => {
const isAsyncQuery = true;
const node: TSESTree.Identifier | TSESTree.Node | undefined =
isCalledUsingSomeObject(queryCall) ? queryCall.parent : queryCall;
if (node) {
if (isCalledInExpect(node, isAsyncQuery)) {
if (
isMemberExpression(node.parent?.parent?.parent?.parent) &&
node.parent?.parent?.parent?.parent.property.type ===
AST_NODE_TYPES.Identifier &&
helpers.isPresenceAssert(node.parent.parent.parent.parent)
) {
return reportError(context, node, 'findBy*');
}
}
}
});
getQueryCalls.forEach((queryCall) => {
const isAsyncQuery = false;
const node: TSESTree.Identifier | TSESTree.Node | undefined =
isCalledUsingSomeObject(queryCall) ? queryCall.parent : queryCall;
if (node) {
if (isCalledInExpect(node, isAsyncQuery)) {
if (
isMemberExpression(node.parent?.parent?.parent) &&
node.parent?.parent?.parent.property.type ===
AST_NODE_TYPES.Identifier &&
helpers.isPresenceAssert(node.parent.parent.parent)
) {
return reportError(context, node, 'getBy*');
}
}
}
});
},
};
},
});