-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathexpect-expect.ts
72 lines (66 loc) · 1.87 KB
/
expect-expect.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
import ESTree from 'estree'
import { dig } from '../utils/ast.js'
import { createRule } from '../utils/createRule.js'
import { parseFnCall } from '../utils/parseFnCall.js'
export default createRule({
create(context) {
const options = {
assertFunctionNames: [] as string[],
...((context.options?.[0] as Record<string, unknown>) ?? {}),
}
const unchecked: ESTree.CallExpression[] = []
function checkExpressions(nodes: ESTree.Node[]) {
for (const node of nodes) {
const index =
node.type === 'CallExpression' ? unchecked.indexOf(node) : -1
if (index !== -1) {
unchecked.splice(index, 1)
break
}
}
}
return {
CallExpression(node) {
const call = parseFnCall(context, node)
if (call?.type === 'test') {
unchecked.push(node)
} else if (
call?.type === 'expect' ||
options.assertFunctionNames.find((name) => dig(node.callee, name))
) {
const ancestors = context.sourceCode.getAncestors(node)
checkExpressions(ancestors)
}
},
'Program:exit'() {
unchecked.forEach((node) => {
context.report({ messageId: 'noAssertions', node: node.callee })
})
},
}
},
meta: {
docs: {
category: 'Best Practices',
description: 'Enforce assertion to be made in a test body',
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/expect-expect.md',
},
messages: {
noAssertions: 'Test has no assertions',
},
schema: [
{
additionalProperties: false,
properties: {
assertFunctionNames: {
items: [{ type: 'string' }],
type: 'array',
},
},
type: 'object',
},
],
type: 'problem',
},
})