/
no-focused-tests.ts
84 lines (75 loc) · 2.12 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
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { createRule, getAccessorValue, parseJestFnCall } from './utils';
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow focused tests',
recommended: 'error',
suggestion: true,
},
messages: {
focusedTest: 'Unexpected focused test.',
suggestRemoveFocus: 'Remove focus from test.',
},
schema: [],
type: 'suggestion',
hasSuggestions: true,
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
const scope = context.getScope();
const jestFnCall = parseJestFnCall(node, scope);
if (jestFnCall?.type !== 'test' && jestFnCall?.type !== 'describe') {
return;
}
if (jestFnCall.name.startsWith('f')) {
context.report({
messageId: 'focusedTest',
node,
suggest: [
{
messageId: 'suggestRemoveFocus',
fix(fixer) {
// don't apply the fixer if we're an aliased import
if (
jestFnCall.head.type === 'import' &&
jestFnCall.name !== jestFnCall.head.local
) {
return null;
}
return fixer.removeRange([node.range[0], node.range[0] + 1]);
},
},
],
});
return;
}
const onlyNode = jestFnCall.members.find(
s => getAccessorValue(s) === 'only',
);
if (!onlyNode) {
return;
}
context.report({
messageId: 'focusedTest',
node: onlyNode,
suggest: [
{
messageId: 'suggestRemoveFocus',
fix: fixer =>
fixer.removeRange([
onlyNode.range[0] - 1,
onlyNode.range[1] +
Number(onlyNode.type !== AST_NODE_TYPES.Identifier),
]),
},
],
});
},
};
},
});