-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathno-conditional-in-test.ts
48 lines (45 loc) · 1.21 KB
/
no-conditional-in-test.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
import type { TSESTree } from '@typescript-eslint/utils';
import { createRule, isTypeOfJestFnCall } from './utils';
export default createRule({
name: __filename,
meta: {
docs: {
description: 'Disallow conditional logic in tests',
category: 'Best Practices',
recommended: false,
},
messages: {
conditionalInTest: 'Avoid having conditionals in tests',
},
type: 'problem',
schema: [],
},
defaultOptions: [],
create(context) {
let inTestCase = false;
const maybeReportConditional = (node: TSESTree.Node) => {
if (inTestCase) {
context.report({
messageId: 'conditionalInTest',
node,
});
}
};
return {
CallExpression(node: TSESTree.CallExpression) {
if (isTypeOfJestFnCall(node, context, ['test'])) {
inTestCase = true;
}
},
'CallExpression:exit'(node) {
if (isTypeOfJestFnCall(node, context, ['test'])) {
inTestCase = false;
}
},
IfStatement: maybeReportConditional,
SwitchStatement: maybeReportConditional,
ConditionalExpression: maybeReportConditional,
LogicalExpression: maybeReportConditional,
};
},
});