forked from testing-library/eslint-plugin-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-wait-for-snapshot.ts
87 lines (79 loc) · 2.04 KB
/
no-wait-for-snapshot.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
86
87
import { ASTUtils, TSESTree } from '@typescript-eslint/utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
findClosestCallExpressionNode,
isMemberExpression,
} from '../node-utils';
export const RULE_NAME = 'no-wait-for-snapshot';
export type MessageIds = 'noWaitForSnapshot';
type Options = [];
const SNAPSHOT_REGEXP = /^(toMatchSnapshot|toMatchInlineSnapshot)$/;
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description:
'Ensures no snapshot is generated inside of a `waitFor` call',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
svelte: 'error',
marko: 'error',
},
},
messages: {
noWaitForSnapshot:
"A snapshot can't be generated inside of a `{{ name }}` call",
},
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
function getClosestAsyncUtil(
node: TSESTree.Node
): TSESTree.Identifier | null {
let n: TSESTree.Node | null = node;
do {
const callExpression = findClosestCallExpressionNode(n);
if (!callExpression) {
return null;
}
if (
ASTUtils.isIdentifier(callExpression.callee) &&
helpers.isAsyncUtil(callExpression.callee)
) {
return callExpression.callee;
}
if (
isMemberExpression(callExpression.callee) &&
ASTUtils.isIdentifier(callExpression.callee.property) &&
helpers.isAsyncUtil(callExpression.callee.property)
) {
return callExpression.callee.property;
}
if (callExpression.parent) {
n = findClosestCallExpressionNode(callExpression.parent);
}
} while (n !== null);
return null;
}
return {
[`Identifier[name=${String(SNAPSHOT_REGEXP)}]`](
node: TSESTree.Identifier
) {
const closestAsyncUtil = getClosestAsyncUtil(node);
if (closestAsyncUtil === null) {
return;
}
context.report({
node,
messageId: 'noWaitForSnapshot',
data: { name: closestAsyncUtil.name },
});
},
};
},
});