/
no-focused-tests.ts
86 lines (77 loc) · 2.22 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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
DescribeAlias,
TestCaseName,
createRule,
isSupportedAccessor,
} from './utils';
const testFunctions = new Set<string>([
DescribeAlias.describe,
TestCaseName.test,
TestCaseName.it,
]);
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(callee.property, 'only');
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow focused tests',
recommended: false,
},
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 });
}
},
}),
});