-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmax-expects.ts
81 lines (73 loc) · 2 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
79
80
81
import * as ESTree from 'estree'
import { getParent } from '../utils/ast.js'
import { createRule } from '../utils/createRule.js'
import { isTypeOfFnCall, parseFnCall } from '../utils/parseFnCall.js'
export default createRule({
create(context) {
const options = {
max: 5,
...((context.options?.[0] as Record<string, unknown>) ?? {}),
}
let count = 0
const maybeResetCount = (node: ESTree.Node) => {
const parent = getParent(node)
const isTestFn =
parent?.type !== 'CallExpression' ||
isTypeOfFnCall(context, parent, ['test'])
if (isTestFn) {
count = 0
}
}
return {
ArrowFunctionExpression: maybeResetCount,
'ArrowFunctionExpression:exit': maybeResetCount,
CallExpression(node) {
const call = parseFnCall(context, node)
if (
call?.type !== 'expect' ||
getParent(call.head.node)?.type === 'MemberExpression'
) {
return
}
count += 1
if (count > options.max) {
context.report({
data: {
count: count.toString(),
max: options.max.toString(),
},
messageId: 'exceededMaxAssertion',
node,
})
}
},
FunctionExpression: maybeResetCount,
'FunctionExpression:exit': maybeResetCount,
}
},
meta: {
docs: {
category: 'Best Practices',
description: 'Enforces a maximum number assertion calls in a test body',
recommended: false,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/max-expects.md',
},
messages: {
exceededMaxAssertion:
'Too many assertion calls ({{ count }}) - maximum allowed is {{ max }}',
},
schema: [
{
additionalProperties: false,
properties: {
max: {
minimum: 1,
type: 'integer',
},
},
type: 'object',
},
],
type: 'suggestion',
},
})