-
Notifications
You must be signed in to change notification settings - Fork 227
/
no-focused-tests.ts
85 lines (75 loc) · 2.3 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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { DescribeAlias, TestCaseName, createRule } from './utils';
const testFunctions = new Set(['describe', 'it', 'test']);
const matchesTestFunction = (
object: TSESTree.LeftHandSideExpression | undefined,
) =>
object &&
'name' in object &&
(object.name in TestCaseName || object.name in DescribeAlias);
const isCallToFocusedTestFunction = (object: TSESTree.Identifier | undefined) =>
object &&
object.name.startsWith('f') &&
testFunctions.has(object.name.substring(1));
const isPropertyNamedOnly = (
property: TSESTree.Expression | TSESTree.Identifier | undefined,
) =>
property &&
(('name' in property && property.name === 'only') ||
('value' in property && property.value === 'only'));
const isCallToTestOnlyFunction = (callee: TSESTree.MemberExpression) =>
matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property);
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;
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 });
}
},
}),
});