-
Notifications
You must be signed in to change notification settings - Fork 234
/
consistent-test-it.ts
159 lines (143 loc) · 4.19 KB
/
consistent-test-it.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
AST_NODE_TYPES,
type TSESLint,
type TSESTree,
} from '@typescript-eslint/utils';
import {
TestCaseName,
createRule,
isTypeOfJestFnCall,
parseJestFnCall,
} from './utils';
const buildFixer =
(
callee: TSESTree.LeftHandSideExpression,
nodeName: string,
preferredTestKeyword: TestCaseName.test | TestCaseName.it,
) =>
(fixer: TSESLint.RuleFixer) => [
fixer.replaceText(
callee.type === AST_NODE_TYPES.MemberExpression ? callee.object : callee,
getPreferredNodeName(nodeName, preferredTestKeyword),
),
];
export default createRule<
[
Partial<{
fn: TestCaseName.it | TestCaseName.test;
withinDescribe: TestCaseName.it | TestCaseName.test;
}>,
],
'consistentMethod' | 'consistentMethodWithinDescribe'
>({
name: __filename,
meta: {
docs: {
description: 'Enforce `test` and `it` usage conventions',
},
fixable: 'code',
messages: {
consistentMethod:
"Prefer using '{{ testKeyword }}' instead of '{{ oppositeTestKeyword }}'",
consistentMethodWithinDescribe:
"Prefer using '{{ testKeywordWithinDescribe }}' instead of '{{ oppositeTestKeyword }}' within describe",
},
schema: [
{
type: 'object',
properties: {
fn: {
type: 'string',
enum: [TestCaseName.it, TestCaseName.test],
},
withinDescribe: {
type: 'string',
enum: [TestCaseName.it, TestCaseName.test],
},
},
additionalProperties: false,
},
],
type: 'suggestion',
},
defaultOptions: [{ fn: TestCaseName.test, withinDescribe: TestCaseName.it }],
create(context) {
const configObj = context.options[0] || {};
const testKeyword = configObj.fn || TestCaseName.test;
const testKeywordWithinDescribe =
configObj.withinDescribe || configObj.fn || TestCaseName.it;
let describeNestingLevel = 0;
return {
CallExpression(node: TSESTree.CallExpression) {
const jestFnCall = parseJestFnCall(node, context);
if (!jestFnCall) {
return;
}
if (jestFnCall.type === 'describe') {
describeNestingLevel++;
return;
}
const funcNode =
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
? node.callee.tag
: node.callee.type === AST_NODE_TYPES.CallExpression
? node.callee.callee
: node.callee;
if (
jestFnCall.type === 'test' &&
describeNestingLevel === 0 &&
!jestFnCall.name.endsWith(testKeyword)
) {
const oppositeTestKeyword = getOppositeTestKeyword(testKeyword);
context.report({
messageId: 'consistentMethod',
node: node.callee,
data: { testKeyword, oppositeTestKeyword },
fix: buildFixer(funcNode, jestFnCall.name, testKeyword),
});
}
if (
jestFnCall.type === 'test' &&
describeNestingLevel > 0 &&
!jestFnCall.name.endsWith(testKeywordWithinDescribe)
) {
const oppositeTestKeyword = getOppositeTestKeyword(
testKeywordWithinDescribe,
);
context.report({
messageId: 'consistentMethodWithinDescribe',
node: node.callee,
data: { testKeywordWithinDescribe, oppositeTestKeyword },
fix: buildFixer(
funcNode,
jestFnCall.name,
testKeywordWithinDescribe,
),
});
}
},
'CallExpression:exit'(node) {
if (isTypeOfJestFnCall(node, context, ['describe'])) {
describeNestingLevel--;
}
},
};
},
});
function getPreferredNodeName(
nodeName: string,
preferredTestKeyword: TestCaseName.test | TestCaseName.it,
) {
if (nodeName === TestCaseName.fit) {
return 'test.only';
}
return nodeName.startsWith('f') || nodeName.startsWith('x')
? nodeName.charAt(0) + preferredTestKeyword
: preferredTestKeyword;
}
function getOppositeTestKeyword(test: TestCaseName.test | TestCaseName.it) {
if (test === TestCaseName.test) {
return TestCaseName.it;
}
return TestCaseName.test;
}