-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathmax-expects.ts
78 lines (70 loc) · 1.78 KB
/
max-expects.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
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import {
type FunctionExpression,
createRule,
isTypeOfJestFnCall,
parseJestFnCall,
} from './utils';
export default createRule({
name: __filename,
meta: {
docs: {
description: 'Enforces a maximum number assertion calls in a test body',
},
messages: {
exceededMaxAssertion:
'Too many assertion calls ({{ count }}) - maximum allowed is {{ max }}',
},
type: 'suggestion',
schema: [
{
type: 'object',
properties: {
max: {
type: 'integer',
minimum: 1,
},
},
additionalProperties: false,
},
],
},
defaultOptions: [{ max: 5 }],
create(context, [{ max }]) {
let count = 0;
const maybeResetCount = (node: FunctionExpression) => {
const isTestFn =
node.parent?.type !== AST_NODE_TYPES.CallExpression ||
isTypeOfJestFnCall(node.parent, context, ['test']);
if (isTestFn) {
count = 0;
}
};
return {
FunctionExpression: maybeResetCount,
'FunctionExpression:exit': maybeResetCount,
ArrowFunctionExpression: maybeResetCount,
'ArrowFunctionExpression:exit': maybeResetCount,
CallExpression(node) {
const jestFnCall = parseJestFnCall(node, context);
if (jestFnCall?.type === 'test') {
count = 0;
}
if (
jestFnCall?.type !== 'expect' ||
jestFnCall.head.node.parent?.type === AST_NODE_TYPES.MemberExpression
) {
return;
}
count += 1;
if (count > max) {
context.report({
node,
messageId: 'exceededMaxAssertion',
data: { count, max },
});
}
},
};
},
});