/
no-focused-tests.ts
105 lines (90 loc) · 2.75 KB
/
no-focused-tests.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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
DescribeAlias,
TestCaseName,
TestCaseProperty,
createRule,
isSupportedAccessor,
} from './utils';
const validTestCaseNames = [TestCaseName.test, TestCaseName.it];
const testFunctions = new Set<string>([
DescribeAlias.describe,
...validTestCaseNames,
]);
interface ConcurrentExpression extends TSESTree.MemberExpressionComputedName {
parent: TSESTree.MemberExpression;
}
const isConcurrentExpression = (
expression: TSESTree.MemberExpression,
): expression is ConcurrentExpression =>
isSupportedAccessor(expression.property, TestCaseProperty.concurrent) &&
!!expression.parent &&
expression.parent.type === AST_NODE_TYPES.MemberExpression;
const matchesTestFunction = (object: TSESTree.LeftHandSideExpression) =>
'name' in object &&
(object.name in TestCaseName || object.name in DescribeAlias);
const isCallToFocusedTestFunction = (object: TSESTree.Identifier) =>
object.name.startsWith('f') && testFunctions.has(object.name.substring(1));
const isCallToTestOnlyFunction = (callee: TSESTree.MemberExpression) =>
matchesTestFunction(callee.object) &&
isSupportedAccessor(
isConcurrentExpression(callee) ? callee.parent.property : callee.property,
'only',
);
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow focused tests',
recommended: 'error',
},
messages: {
focusedTest: 'Unexpected focused test.',
},
fixable: 'code',
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create: context => ({
CallExpression(node) {
const callee =
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
? node.callee.tag
: node.callee;
if (callee.type === AST_NODE_TYPES.MemberExpression) {
if (
callee.object.type === AST_NODE_TYPES.Identifier &&
isCallToFocusedTestFunction(callee.object)
) {
context.report({ messageId: 'focusedTest', node: callee.object });
return;
}
if (
callee.object.type === AST_NODE_TYPES.MemberExpression &&
isCallToTestOnlyFunction(callee.object)
) {
context.report({
messageId: 'focusedTest',
node: callee.object.property,
});
return;
}
if (isCallToTestOnlyFunction(callee)) {
context.report({ messageId: 'focusedTest', node: callee.property });
return;
}
}
if (
callee.type === AST_NODE_TYPES.Identifier &&
isCallToFocusedTestFunction(callee)
) {
context.report({ messageId: 'focusedTest', node: callee });
}
},
}),
});