/
valid-expect-in-promise.ts
218 lines (192 loc) · 6.04 KB
/
valid-expect-in-promise.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
AST_NODE_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
CalledKnownMemberExpression,
FunctionExpression,
KnownCallExpression,
TestCaseName,
createRule,
getAccessorValue,
isExpectMember,
isFunction,
isSupportedAccessor,
} from './utils';
type MessageIds = 'returnPromise';
type RuleContext = TSESLint.RuleContext<MessageIds, unknown[]>;
type ThenOrCatchCallExpression = KnownCallExpression<'then' | 'catch'>;
const isThenOrCatchCall = (
node: TSESTree.Node,
): node is ThenOrCatchCallExpression =>
node.type === AST_NODE_TYPES.CallExpression &&
node.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.callee.property) &&
['then', 'catch'].includes(getAccessorValue(node.callee.property));
const isExpectCallPresentInFunction = (body: TSESTree.Node) => {
if (body.type === AST_NODE_TYPES.BlockStatement) {
return body.body.find(line => {
if (line.type === AST_NODE_TYPES.ExpressionStatement) {
return isFullExpectCall(line.expression);
}
if (line.type === AST_NODE_TYPES.ReturnStatement && line.argument) {
return isFullExpectCall(line.argument);
}
return false;
});
}
return isFullExpectCall(body);
};
const isFullExpectCall = (expression: TSESTree.Node) =>
expression.type === AST_NODE_TYPES.CallExpression &&
isExpectMember(expression.callee);
const reportReturnRequired = (context: RuleContext, node: TSESTree.Node) => {
context.report({
loc: {
end: {
column: node.loc.end.column,
line: node.loc.end.line,
},
start: node.loc.start,
},
messageId: 'returnPromise',
node,
});
};
const isPromiseReturnedLater = (
node: TSESTree.Node,
testFunctionBody: TSESTree.Statement[],
) => {
let promiseName;
if (
node.type === AST_NODE_TYPES.ExpressionStatement &&
node.expression.type === AST_NODE_TYPES.CallExpression &&
node.expression.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.expression.callee.object)
) {
promiseName = getAccessorValue(node.expression.callee.object);
} else if (
node.type === AST_NODE_TYPES.VariableDeclarator &&
node.id.type === AST_NODE_TYPES.Identifier
) {
promiseName = node.id.name;
}
const lastLineInTestFunc = testFunctionBody[testFunctionBody.length - 1];
return (
lastLineInTestFunc.type === AST_NODE_TYPES.ReturnStatement &&
lastLineInTestFunc.argument &&
(('name' in lastLineInTestFunc.argument &&
lastLineInTestFunc.argument.name === promiseName) ||
!promiseName)
);
};
const isTestFunc = (node: TSESTree.Node) =>
node.type === AST_NODE_TYPES.CallExpression &&
isSupportedAccessor(node.callee) &&
([TestCaseName.it, TestCaseName.test] as string[]).includes(
getAccessorValue(node.callee),
);
const findTestFunction = (node: TSESTree.Node | undefined) => {
while (node) {
if (isFunction(node) && node.parent && isTestFunc(node.parent)) {
return node;
}
node = node.parent;
}
return null;
};
const isParentThenOrPromiseReturned = (
node: TSESTree.Node,
testFunctionBody: TSESTree.Statement[],
) =>
node.type === AST_NODE_TYPES.ReturnStatement ||
isPromiseReturnedLater(node, testFunctionBody);
// prettier-ignore
// WEB-40105
type PromiseCallbacks = [
TSESTree.Expression | undefined,
TSESTree.Expression | undefined
]
const verifyExpectWithReturn = (
promiseCallbacks: PromiseCallbacks,
node: CalledKnownMemberExpression<'then' | 'catch'>,
context: RuleContext,
testFunctionBody: TSESTree.Statement[],
) => {
promiseCallbacks.some(promiseCallback => {
if (
promiseCallback &&
isFunction(promiseCallback) &&
promiseCallback.body
) {
if (
isExpectCallPresentInFunction(promiseCallback.body) &&
node.parent &&
node.parent.parent &&
!isParentThenOrPromiseReturned(node.parent.parent, testFunctionBody)
) {
reportReturnRequired(context, node.parent.parent);
return true;
}
}
return false;
});
};
const isHavingAsyncCallBackParam = (testFunction: FunctionExpression) =>
testFunction.params[0] &&
testFunction.params[0].type === AST_NODE_TYPES.Identifier;
export default createRule<unknown[], MessageIds>({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Enforce having return statement when testing with promises',
recommended: 'error',
},
messages: {
returnPromise:
'Promise should be returned to test its fulfillment or rejection',
},
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (
!isThenOrCatchCall(node) ||
(node.parent && node.parent.type === AST_NODE_TYPES.AwaitExpression)
) {
return;
}
const testFunction = findTestFunction(node);
if (testFunction && !isHavingAsyncCallBackParam(testFunction)) {
const { body } = testFunction;
/* istanbul ignore if https://github.com/typescript-eslint/typescript-eslint/issues/734 */
if (!body) {
throw new Error(
`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
);
}
if (body.type !== AST_NODE_TYPES.BlockStatement) {
return;
}
const testFunctionBody = body.body;
const [fulfillmentCallback, rejectionCallback] = node.arguments;
// then block can have two args, fulfillment & rejection
// then block can have one args, fulfillment
// catch block can have one args, rejection
// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
verifyExpectWithReturn(
[fulfillmentCallback, rejectionCallback],
node.callee,
context,
testFunctionBody,
);
}
},
};
},
});