eslint-plugin-jest 26.4.7 → 26.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/lib/rules/consistent-test-it.js +2 -3
- package/lib/rules/expect-expect.js +2 -2
- package/lib/rules/max-nested-describe.js +2 -2
- package/lib/rules/no-conditional-expect.js +3 -3
- package/lib/rules/no-conditional-in-test.js +2 -2
- package/lib/rules/no-disabled-tests.js +2 -2
- package/lib/rules/no-done-callback.js +3 -3
- package/lib/rules/no-duplicate-hooks.js +2 -3
- package/lib/rules/no-export.js +1 -1
- package/lib/rules/no-focused-tests.js +1 -2
- package/lib/rules/no-hooks.js +1 -1
- package/lib/rules/no-identical-title.js +2 -3
- package/lib/rules/no-if.js +2 -2
- package/lib/rules/no-standalone-expect.js +5 -5
- package/lib/rules/no-test-prefixes.js +1 -2
- package/lib/rules/no-test-return-statement.js +2 -2
- package/lib/rules/prefer-expect-assertions.js +2 -2
- package/lib/rules/prefer-hooks-in-order.js +2 -2
- package/lib/rules/prefer-hooks-on-top.js +2 -4
- package/lib/rules/prefer-lowercase-title.js +2 -3
- package/lib/rules/prefer-snapshot-hint.js +2 -6
- package/lib/rules/prefer-todo.js +1 -1
- package/lib/rules/require-hook.js +7 -7
- package/lib/rules/require-top-level-describe.js +2 -3
- package/lib/rules/utils/misc.js +2 -2
- package/lib/rules/utils/parseJestFnCall.js +20 -7
- package/lib/rules/valid-describe-callback.js +1 -1
- package/lib/rules/valid-expect-in-promise.js +7 -7
- package/lib/rules/valid-title.js +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,6 +59,25 @@ doing:
|
|
|
59
59
|
This is included in all configs shared by this plugin, so can be omitted if
|
|
60
60
|
extending them.
|
|
61
61
|
|
|
62
|
+
#### Aliased Jest globals
|
|
63
|
+
|
|
64
|
+
You can tell this plugin about any global Jests you have aliased using the
|
|
65
|
+
`globalAliases` setting:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"settings": {
|
|
70
|
+
"jest": {
|
|
71
|
+
"globalAliases": {
|
|
72
|
+
"describe": ["context"],
|
|
73
|
+
"fdescribe": ["fcontext"],
|
|
74
|
+
"xdescribe": ["xcontext"]
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
62
81
|
### Running rules only on test-related files
|
|
63
82
|
|
|
64
83
|
The rules provided by this plugin assume that the files they are checking are
|
|
@@ -50,8 +50,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
50
50
|
let describeNestingLevel = 0;
|
|
51
51
|
return {
|
|
52
52
|
CallExpression(node) {
|
|
53
|
-
const
|
|
54
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node, scope);
|
|
53
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
55
54
|
|
|
56
55
|
if (!jestFnCall) {
|
|
57
56
|
return;
|
|
@@ -92,7 +91,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
92
91
|
},
|
|
93
92
|
|
|
94
93
|
'CallExpression:exit'(node) {
|
|
95
|
-
if ((0, _utils2.isTypeOfJestFnCall)(node, context
|
|
94
|
+
if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['describe'])) {
|
|
96
95
|
describeNestingLevel--;
|
|
97
96
|
}
|
|
98
97
|
}
|
|
@@ -76,7 +76,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
76
76
|
|
|
77
77
|
if (node.type === _utils.AST_NODE_TYPES.FunctionDeclaration) {
|
|
78
78
|
const declaredVariables = context.getDeclaredVariables(node);
|
|
79
|
-
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context
|
|
79
|
+
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context);
|
|
80
80
|
checkCallExpressionUsed(testCallExpressions);
|
|
81
81
|
}
|
|
82
82
|
|
|
@@ -93,7 +93,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
93
93
|
|
|
94
94
|
const name = (_getNodeName = (0, _utils2.getNodeName)(node.callee)) !== null && _getNodeName !== void 0 ? _getNodeName : '';
|
|
95
95
|
|
|
96
|
-
if ((0, _utils2.isTypeOfJestFnCall)(node, context
|
|
96
|
+
if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test']) || additionalTestBlockFunctions.includes(name)) {
|
|
97
97
|
if (node.callee.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.callee.property, 'todo')) {
|
|
98
98
|
return;
|
|
99
99
|
}
|
|
@@ -46,7 +46,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
46
46
|
parent
|
|
47
47
|
} = node;
|
|
48
48
|
|
|
49
|
-
if ((parent === null || parent === void 0 ? void 0 : parent.type) !== _utils.AST_NODE_TYPES.CallExpression || !(0, _utils2.isTypeOfJestFnCall)(parent, context
|
|
49
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) !== _utils.AST_NODE_TYPES.CallExpression || !(0, _utils2.isTypeOfJestFnCall)(parent, context, ['describe'])) {
|
|
50
50
|
return;
|
|
51
51
|
}
|
|
52
52
|
|
|
@@ -69,7 +69,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
69
69
|
parent
|
|
70
70
|
} = node;
|
|
71
71
|
|
|
72
|
-
if ((parent === null || parent === void 0 ? void 0 : parent.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isTypeOfJestFnCall)(parent, context
|
|
72
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isTypeOfJestFnCall)(parent, context, ['describe'])) {
|
|
73
73
|
describeCallbackStack.pop();
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -39,7 +39,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
39
39
|
return {
|
|
40
40
|
FunctionDeclaration(node) {
|
|
41
41
|
const declaredVariables = context.getDeclaredVariables(node);
|
|
42
|
-
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context
|
|
42
|
+
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context);
|
|
43
43
|
|
|
44
44
|
if (testCallExpressions.length > 0) {
|
|
45
45
|
inTestCase = true;
|
|
@@ -47,7 +47,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
47
47
|
},
|
|
48
48
|
|
|
49
49
|
CallExpression(node) {
|
|
50
|
-
if ((0, _utils2.isTypeOfJestFnCall)(node, context
|
|
50
|
+
if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
51
51
|
inTestCase = true;
|
|
52
52
|
}
|
|
53
53
|
|
|
@@ -71,7 +71,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
71
71
|
},
|
|
72
72
|
|
|
73
73
|
'CallExpression:exit'(node) {
|
|
74
|
-
if ((0, _utils2.isTypeOfJestFnCall)(node, context
|
|
74
|
+
if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
75
75
|
inTestCase = false;
|
|
76
76
|
}
|
|
77
77
|
|
|
@@ -37,13 +37,13 @@ var _default = (0, _utils.createRule)({
|
|
|
37
37
|
|
|
38
38
|
return {
|
|
39
39
|
CallExpression(node) {
|
|
40
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, context
|
|
40
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
41
41
|
inTestCase = true;
|
|
42
42
|
}
|
|
43
43
|
},
|
|
44
44
|
|
|
45
45
|
'CallExpression:exit'(node) {
|
|
46
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, context
|
|
46
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
47
47
|
inTestCase = false;
|
|
48
48
|
}
|
|
49
49
|
},
|
|
@@ -33,7 +33,7 @@ var _default = (0, _utils.createRule)({
|
|
|
33
33
|
let testDepth = 0;
|
|
34
34
|
return {
|
|
35
35
|
CallExpression(node) {
|
|
36
|
-
const jestFnCall = (0, _utils.parseJestFnCall)(node, context
|
|
36
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
37
37
|
|
|
38
38
|
if (!jestFnCall) {
|
|
39
39
|
return;
|
|
@@ -64,7 +64,7 @@ var _default = (0, _utils.createRule)({
|
|
|
64
64
|
},
|
|
65
65
|
|
|
66
66
|
'CallExpression:exit'(node) {
|
|
67
|
-
const jestFnCall = (0, _utils.parseJestFnCall)(node, context
|
|
67
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
68
68
|
|
|
69
69
|
if (!jestFnCall) {
|
|
70
70
|
return;
|
|
@@ -9,12 +9,12 @@ var _utils = require("@typescript-eslint/utils");
|
|
|
9
9
|
|
|
10
10
|
var _utils2 = require("./utils");
|
|
11
11
|
|
|
12
|
-
const findCallbackArg = (node, isJestEach,
|
|
12
|
+
const findCallbackArg = (node, isJestEach, context) => {
|
|
13
13
|
if (isJestEach) {
|
|
14
14
|
return node.arguments[1];
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node,
|
|
17
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
18
18
|
|
|
19
19
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'hook' && node.arguments.length >= 1) {
|
|
20
20
|
return node.arguments[0];
|
|
@@ -62,7 +62,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
const callback = findCallbackArg(node, isJestEach, context
|
|
65
|
+
const callback = findCallbackArg(node, isJestEach, context);
|
|
66
66
|
const callbackArgIndex = Number(isJestEach);
|
|
67
67
|
|
|
68
68
|
if (!callback || !(0, _utils2.isFunction)(callback) || callback.params.length !== 1 + callbackArgIndex) {
|
|
@@ -29,8 +29,7 @@ var _default = (0, _utils.createRule)({
|
|
|
29
29
|
CallExpression(node) {
|
|
30
30
|
var _jestFnCall$name;
|
|
31
31
|
|
|
32
|
-
const
|
|
33
|
-
const jestFnCall = (0, _utils.parseJestFnCall)(node, scope);
|
|
32
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
34
33
|
|
|
35
34
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'describe') {
|
|
36
35
|
hookContexts.push({});
|
|
@@ -56,7 +55,7 @@ var _default = (0, _utils.createRule)({
|
|
|
56
55
|
},
|
|
57
56
|
|
|
58
57
|
'CallExpression:exit'(node) {
|
|
59
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, context
|
|
58
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['describe'])) {
|
|
60
59
|
hookContexts.pop();
|
|
61
60
|
}
|
|
62
61
|
}
|
package/lib/rules/no-export.js
CHANGED
|
@@ -31,8 +31,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
31
31
|
create(context) {
|
|
32
32
|
return {
|
|
33
33
|
CallExpression(node) {
|
|
34
|
-
const
|
|
35
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node, scope);
|
|
34
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
36
35
|
|
|
37
36
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test' && (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'describe') {
|
|
38
37
|
return;
|
package/lib/rules/no-hooks.js
CHANGED
|
@@ -39,7 +39,7 @@ var _default = (0, _utils.createRule)({
|
|
|
39
39
|
}]) {
|
|
40
40
|
return {
|
|
41
41
|
CallExpression(node) {
|
|
42
|
-
const jestFnCall = (0, _utils.parseJestFnCall)(node, context
|
|
42
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
43
43
|
|
|
44
44
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'hook' && !allow.includes(jestFnCall.name)) {
|
|
45
45
|
context.report({
|
|
@@ -33,9 +33,8 @@ var _default = (0, _utils.createRule)({
|
|
|
33
33
|
const contexts = [newDescribeContext()];
|
|
34
34
|
return {
|
|
35
35
|
CallExpression(node) {
|
|
36
|
-
const scope = context.getScope();
|
|
37
36
|
const currentLayer = contexts[contexts.length - 1];
|
|
38
|
-
const jestFnCall = (0, _utils.parseJestFnCall)(node,
|
|
37
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
39
38
|
|
|
40
39
|
if (!jestFnCall) {
|
|
41
40
|
return;
|
|
@@ -83,7 +82,7 @@ var _default = (0, _utils.createRule)({
|
|
|
83
82
|
},
|
|
84
83
|
|
|
85
84
|
'CallExpression:exit'(node) {
|
|
86
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, context
|
|
85
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['describe'])) {
|
|
87
86
|
contexts.pop();
|
|
88
87
|
}
|
|
89
88
|
}
|
package/lib/rules/no-if.js
CHANGED
|
@@ -58,7 +58,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
58
58
|
|
|
59
59
|
return {
|
|
60
60
|
CallExpression(node) {
|
|
61
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context
|
|
61
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
62
62
|
|
|
63
63
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) === 'test') {
|
|
64
64
|
stack.push(true);
|
|
@@ -75,7 +75,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
75
75
|
|
|
76
76
|
FunctionDeclaration(node) {
|
|
77
77
|
const declaredVariables = context.getDeclaredVariables(node);
|
|
78
|
-
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context
|
|
78
|
+
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context);
|
|
79
79
|
stack.push(testCallExpressions.length > 0);
|
|
80
80
|
},
|
|
81
81
|
|
|
@@ -9,7 +9,7 @@ var _utils = require("@typescript-eslint/utils");
|
|
|
9
9
|
|
|
10
10
|
var _utils2 = require("./utils");
|
|
11
11
|
|
|
12
|
-
const getBlockType = (statement,
|
|
12
|
+
const getBlockType = (statement, context) => {
|
|
13
13
|
const func = statement.parent;
|
|
14
14
|
/* istanbul ignore if */
|
|
15
15
|
|
|
@@ -30,7 +30,7 @@ const getBlockType = (statement, scope) => {
|
|
|
30
30
|
} // if it's not a variable, it will be callExpr, we only care about describe
|
|
31
31
|
|
|
32
32
|
|
|
33
|
-
if (expr.type === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isTypeOfJestFnCall)(expr,
|
|
33
|
+
if (expr.type === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isTypeOfJestFnCall)(expr, context, ['describe'])) {
|
|
34
34
|
return 'describe';
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -73,7 +73,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
73
73
|
|
|
74
74
|
const isCustomTestBlockFunction = node => additionalTestBlockFunctions.includes((0, _utils2.getNodeName)(node) || '');
|
|
75
75
|
|
|
76
|
-
const isTestBlock = node => (0, _utils2.isTypeOfJestFnCall)(node, context
|
|
76
|
+
const isTestBlock = node => (0, _utils2.isTypeOfJestFnCall)(node, context, ['test']) || isCustomTestBlockFunction(node);
|
|
77
77
|
|
|
78
78
|
return {
|
|
79
79
|
CallExpression(node) {
|
|
@@ -108,7 +108,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
108
108
|
},
|
|
109
109
|
|
|
110
110
|
BlockStatement(statement) {
|
|
111
|
-
const blockType = getBlockType(statement, context
|
|
111
|
+
const blockType = getBlockType(statement, context);
|
|
112
112
|
|
|
113
113
|
if (blockType) {
|
|
114
114
|
callStack.push(blockType);
|
|
@@ -116,7 +116,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
116
116
|
},
|
|
117
117
|
|
|
118
118
|
'BlockStatement:exit'(statement) {
|
|
119
|
-
if (callStack[callStack.length - 1] === getBlockType(statement, context
|
|
119
|
+
if (callStack[callStack.length - 1] === getBlockType(statement, context)) {
|
|
120
120
|
callStack.pop();
|
|
121
121
|
}
|
|
122
122
|
},
|
|
@@ -29,8 +29,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
29
29
|
create(context) {
|
|
30
30
|
return {
|
|
31
31
|
CallExpression(node) {
|
|
32
|
-
const
|
|
33
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node, scope);
|
|
32
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
34
33
|
|
|
35
34
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'describe' && (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test') {
|
|
36
35
|
return;
|
|
@@ -38,7 +38,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
38
38
|
create(context) {
|
|
39
39
|
return {
|
|
40
40
|
CallExpression(node) {
|
|
41
|
-
if (!(0, _utils2.isTypeOfJestFnCall)(node, context
|
|
41
|
+
if (!(0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -53,7 +53,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
53
53
|
|
|
54
54
|
FunctionDeclaration(node) {
|
|
55
55
|
const declaredVariables = context.getDeclaredVariables(node);
|
|
56
|
-
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context
|
|
56
|
+
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables, context);
|
|
57
57
|
if (testCallExpressions.length === 0) return;
|
|
58
58
|
const returnStmt = node.body.body.find(t => t.type === _utils.AST_NODE_TYPES.ReturnStatement);
|
|
59
59
|
if (!returnStmt) return;
|
|
@@ -116,7 +116,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
116
116
|
'ForOfStatement:exit': exitForLoop,
|
|
117
117
|
|
|
118
118
|
CallExpression(node) {
|
|
119
|
-
if ((0, _utils2.isTypeOfJestFnCall)(node, context
|
|
119
|
+
if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
120
120
|
inTestCaseCall = true;
|
|
121
121
|
return;
|
|
122
122
|
}
|
|
@@ -133,7 +133,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
133
133
|
},
|
|
134
134
|
|
|
135
135
|
'CallExpression:exit'(node) {
|
|
136
|
-
if (!(0, _utils2.isTypeOfJestFnCall)(node, context
|
|
136
|
+
if (!(0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
137
137
|
return;
|
|
138
138
|
}
|
|
139
139
|
|
|
@@ -35,7 +35,7 @@ var _default = (0, _utils.createRule)({
|
|
|
35
35
|
return;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
const jestFnCall = (0, _utils.parseJestFnCall)(node, context
|
|
38
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
39
39
|
|
|
40
40
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'hook') {
|
|
41
41
|
// Reset the previousHookIndex when encountering something different from a hook
|
|
@@ -63,7 +63,7 @@ var _default = (0, _utils.createRule)({
|
|
|
63
63
|
},
|
|
64
64
|
|
|
65
65
|
'CallExpression:exit'(node) {
|
|
66
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, context
|
|
66
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['hook'])) {
|
|
67
67
|
inHook = false;
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
@@ -27,13 +27,11 @@ var _default = (0, _utils.createRule)({
|
|
|
27
27
|
const hooksContext = [false];
|
|
28
28
|
return {
|
|
29
29
|
CallExpression(node) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, scope, ['test'])) {
|
|
30
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
33
31
|
hooksContext[hooksContext.length - 1] = true;
|
|
34
32
|
}
|
|
35
33
|
|
|
36
|
-
if (hooksContext[hooksContext.length - 1] && (0, _utils.isTypeOfJestFnCall)(node,
|
|
34
|
+
if (hooksContext[hooksContext.length - 1] && (0, _utils.isTypeOfJestFnCall)(node, context, ['hook'])) {
|
|
37
35
|
context.report({
|
|
38
36
|
messageId: 'noHookOnTop',
|
|
39
37
|
node
|
|
@@ -80,8 +80,7 @@ var _default = (0, _utils.createRule)({
|
|
|
80
80
|
let numberOfDescribeBlocks = 0;
|
|
81
81
|
return {
|
|
82
82
|
CallExpression(node) {
|
|
83
|
-
const
|
|
84
|
-
const jestFnCall = (0, _utils.parseJestFnCall)(node, scope);
|
|
83
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
85
84
|
|
|
86
85
|
if (!jestFnCall || !hasStringAsFirstArgument(node)) {
|
|
87
86
|
return;
|
|
@@ -128,7 +127,7 @@ var _default = (0, _utils.createRule)({
|
|
|
128
127
|
},
|
|
129
128
|
|
|
130
129
|
'CallExpression:exit'(node) {
|
|
131
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, context
|
|
130
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['describe'])) {
|
|
132
131
|
numberOfDescribeBlocks--;
|
|
133
132
|
}
|
|
134
133
|
}
|
|
@@ -106,9 +106,7 @@ var _default = (0, _utils.createRule)({
|
|
|
106
106
|
'ArrowFunctionExpression:exit': exitExpression,
|
|
107
107
|
|
|
108
108
|
'CallExpression:exit'(node) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, scope, ['describe', 'test'])) {
|
|
109
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['describe', 'test'])) {
|
|
112
110
|
var _depths$pop;
|
|
113
111
|
|
|
114
112
|
/* istanbul ignore next */
|
|
@@ -117,9 +115,7 @@ var _default = (0, _utils.createRule)({
|
|
|
117
115
|
},
|
|
118
116
|
|
|
119
117
|
CallExpression(node) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, scope, ['describe', 'test'])) {
|
|
118
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['describe', 'test'])) {
|
|
123
119
|
depths.push(expressionDepth);
|
|
124
120
|
expressionDepth = 0;
|
|
125
121
|
}
|
package/lib/rules/prefer-todo.js
CHANGED
|
@@ -60,7 +60,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
60
60
|
return {
|
|
61
61
|
CallExpression(node) {
|
|
62
62
|
const [title, callback] = node.arguments;
|
|
63
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context
|
|
63
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
64
64
|
|
|
65
65
|
if (!title || (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test' || !isTargetedTestCase(jestFnCall) || !(0, _utils2.isStringNode)(title)) {
|
|
66
66
|
return;
|
|
@@ -9,10 +9,10 @@ var _utils = require("@typescript-eslint/utils");
|
|
|
9
9
|
|
|
10
10
|
var _utils2 = require("./utils");
|
|
11
11
|
|
|
12
|
-
const isJestFnCall = (node,
|
|
12
|
+
const isJestFnCall = (node, context) => {
|
|
13
13
|
var _getNodeName;
|
|
14
14
|
|
|
15
|
-
if ((0, _utils2.parseJestFnCall)(node,
|
|
15
|
+
if ((0, _utils2.parseJestFnCall)(node, context)) {
|
|
16
16
|
return true;
|
|
17
17
|
}
|
|
18
18
|
|
|
@@ -23,13 +23,13 @@ const isNullOrUndefined = node => {
|
|
|
23
23
|
return node.type === _utils.AST_NODE_TYPES.Literal && node.value === null || (0, _utils2.isIdentifier)(node, 'undefined');
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
const shouldBeInHook = (node,
|
|
26
|
+
const shouldBeInHook = (node, context, allowedFunctionCalls = []) => {
|
|
27
27
|
switch (node.type) {
|
|
28
28
|
case _utils.AST_NODE_TYPES.ExpressionStatement:
|
|
29
|
-
return shouldBeInHook(node.expression,
|
|
29
|
+
return shouldBeInHook(node.expression, context, allowedFunctionCalls);
|
|
30
30
|
|
|
31
31
|
case _utils.AST_NODE_TYPES.CallExpression:
|
|
32
|
-
return !(isJestFnCall(node,
|
|
32
|
+
return !(isJestFnCall(node, context) || allowedFunctionCalls.includes((0, _utils2.getNodeName)(node)));
|
|
33
33
|
|
|
34
34
|
case _utils.AST_NODE_TYPES.VariableDeclaration:
|
|
35
35
|
{
|
|
@@ -85,7 +85,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
85
85
|
|
|
86
86
|
const checkBlockBody = body => {
|
|
87
87
|
for (const statement of body) {
|
|
88
|
-
if (shouldBeInHook(statement, context
|
|
88
|
+
if (shouldBeInHook(statement, context, allowedFunctionCalls)) {
|
|
89
89
|
context.report({
|
|
90
90
|
node: statement,
|
|
91
91
|
messageId: 'useHook'
|
|
@@ -100,7 +100,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
100
100
|
},
|
|
101
101
|
|
|
102
102
|
CallExpression(node) {
|
|
103
|
-
if (!(0, _utils2.isTypeOfJestFnCall)(node, context
|
|
103
|
+
if (!(0, _utils2.isTypeOfJestFnCall)(node, context, ['describe']) || node.arguments.length < 2) {
|
|
104
104
|
return;
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -46,8 +46,7 @@ var _default = (0, _utils.createRule)({
|
|
|
46
46
|
let numberOfDescribeBlocks = 0;
|
|
47
47
|
return {
|
|
48
48
|
CallExpression(node) {
|
|
49
|
-
const
|
|
50
|
-
const jestFnCall = (0, _utils.parseJestFnCall)(node, scope);
|
|
49
|
+
const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
|
|
51
50
|
|
|
52
51
|
if (!jestFnCall) {
|
|
53
52
|
return;
|
|
@@ -94,7 +93,7 @@ var _default = (0, _utils.createRule)({
|
|
|
94
93
|
},
|
|
95
94
|
|
|
96
95
|
'CallExpression:exit'(node) {
|
|
97
|
-
if ((0, _utils.isTypeOfJestFnCall)(node, context
|
|
96
|
+
if ((0, _utils.isTypeOfJestFnCall)(node, context, ['describe'])) {
|
|
98
97
|
numberOfDescribeBlocks--;
|
|
99
98
|
}
|
|
100
99
|
}
|
package/lib/rules/utils/misc.js
CHANGED
|
@@ -96,12 +96,12 @@ const isFunction = node => node.type === _utils.AST_NODE_TYPES.FunctionExpressio
|
|
|
96
96
|
|
|
97
97
|
exports.isFunction = isFunction;
|
|
98
98
|
|
|
99
|
-
const getTestCallExpressionsFromDeclaredVariables = (declaredVariables,
|
|
99
|
+
const getTestCallExpressionsFromDeclaredVariables = (declaredVariables, context) => {
|
|
100
100
|
return declaredVariables.reduce((acc, {
|
|
101
101
|
references
|
|
102
102
|
}) => acc.concat(references.map(({
|
|
103
103
|
identifier
|
|
104
|
-
}) => identifier.parent).filter(node => (node === null || node === void 0 ? void 0 : node.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _parseJestFnCall.isTypeOfJestFnCall)(node,
|
|
104
|
+
}) => identifier.parent).filter(node => (node === null || node === void 0 ? void 0 : node.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _parseJestFnCall.isTypeOfJestFnCall)(node, context, ['test']))), []);
|
|
105
105
|
};
|
|
106
106
|
/**
|
|
107
107
|
* Replaces an accessor node with the given `text`, surrounding it in quotes if required.
|
|
@@ -10,8 +10,8 @@ var _utils = require("@typescript-eslint/utils");
|
|
|
10
10
|
|
|
11
11
|
var _utils2 = require("../utils");
|
|
12
12
|
|
|
13
|
-
const isTypeOfJestFnCall = (node,
|
|
14
|
-
const jestFnCall = parseJestFnCall(node,
|
|
13
|
+
const isTypeOfJestFnCall = (node, context, types) => {
|
|
14
|
+
const jestFnCall = parseJestFnCall(node, context);
|
|
15
15
|
return jestFnCall !== null && types.includes(jestFnCall.type);
|
|
16
16
|
};
|
|
17
17
|
|
|
@@ -67,7 +67,20 @@ const determineJestFnType = name => {
|
|
|
67
67
|
|
|
68
68
|
const ValidJestFnCallChains = ['afterAll', 'afterEach', 'beforeAll', 'beforeEach', 'describe', 'describe.each', 'describe.only', 'describe.only.each', 'describe.skip', 'describe.skip.each', 'fdescribe', 'fdescribe.each', 'xdescribe', 'xdescribe.each', 'it', 'it.concurrent', 'it.concurrent.each', 'it.concurrent.only.each', 'it.concurrent.skip.each', 'it.each', 'it.failing', 'it.only', 'it.only.each', 'it.only.failing', 'it.skip', 'it.skip.each', 'it.skip.failing', 'it.todo', 'fit', 'fit.each', 'fit.failing', 'xit', 'xit.each', 'xit.failing', 'test', 'test.concurrent', 'test.concurrent.each', 'test.concurrent.only.each', 'test.concurrent.skip.each', 'test.each', 'test.failing', 'test.only', 'test.only.each', 'test.only.failing', 'test.skip', 'test.skip.each', 'test.skip.failing', 'test.todo', 'xtest', 'xtest.each', 'xtest.failing'];
|
|
69
69
|
|
|
70
|
-
const
|
|
70
|
+
const resolvePossibleAliasedGlobal = (global, context) => {
|
|
71
|
+
var _context$settings$jes, _context$settings$jes2;
|
|
72
|
+
|
|
73
|
+
const globalAliases = (_context$settings$jes = (_context$settings$jes2 = context.settings.jest) === null || _context$settings$jes2 === void 0 ? void 0 : _context$settings$jes2.globalAliases) !== null && _context$settings$jes !== void 0 ? _context$settings$jes : {};
|
|
74
|
+
const alias = Object.entries(globalAliases).find(([, aliases]) => aliases.includes(global));
|
|
75
|
+
|
|
76
|
+
if (alias) {
|
|
77
|
+
return alias[0];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return null;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const parseJestFnCall = (node, context) => {
|
|
71
84
|
var _node$parent, _node$parent2, _resolved$original;
|
|
72
85
|
|
|
73
86
|
// ensure that we're at the "top" of the function call chain otherwise when
|
|
@@ -105,7 +118,7 @@ const parseJestFnCall = (node, scope) => {
|
|
|
105
118
|
return null;
|
|
106
119
|
}
|
|
107
120
|
|
|
108
|
-
const resolved = resolveToJestFn(
|
|
121
|
+
const resolved = resolveToJestFn(context, (0, _utils2.getAccessorValue)(first)); // we're not a jest function
|
|
109
122
|
|
|
110
123
|
if (!resolved) {
|
|
111
124
|
return null;
|
|
@@ -266,8 +279,8 @@ const collectReferences = scope => {
|
|
|
266
279
|
};
|
|
267
280
|
};
|
|
268
281
|
|
|
269
|
-
const resolveToJestFn = (
|
|
270
|
-
const references = collectReferences(
|
|
282
|
+
const resolveToJestFn = (context, identifier) => {
|
|
283
|
+
const references = collectReferences(context.getScope());
|
|
271
284
|
const maybeImport = references.imports.get(identifier);
|
|
272
285
|
|
|
273
286
|
if (maybeImport) {
|
|
@@ -291,7 +304,7 @@ const resolveToJestFn = (scope, identifier) => {
|
|
|
291
304
|
}
|
|
292
305
|
|
|
293
306
|
return {
|
|
294
|
-
original:
|
|
307
|
+
original: resolvePossibleAliasedGlobal(identifier, context),
|
|
295
308
|
local: identifier,
|
|
296
309
|
type: 'global'
|
|
297
310
|
};
|
|
@@ -41,7 +41,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
41
41
|
create(context) {
|
|
42
42
|
return {
|
|
43
43
|
CallExpression(node) {
|
|
44
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context
|
|
44
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
45
45
|
|
|
46
46
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'describe') {
|
|
47
47
|
return;
|
|
@@ -52,8 +52,8 @@ const findTopMostCallExpression = node => {
|
|
|
52
52
|
return topMostCallExpression;
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
-
const isTestCaseCallWithCallbackArg = (node,
|
|
56
|
-
const jestCallFn = (0, _utils2.parseJestFnCall)(node,
|
|
55
|
+
const isTestCaseCallWithCallbackArg = (node, context) => {
|
|
56
|
+
const jestCallFn = (0, _utils2.parseJestFnCall)(node, context);
|
|
57
57
|
|
|
58
58
|
if ((jestCallFn === null || jestCallFn === void 0 ? void 0 : jestCallFn.type) !== 'test') {
|
|
59
59
|
return false;
|
|
@@ -252,7 +252,7 @@ const findFirstBlockBodyUp = node => {
|
|
|
252
252
|
throw new Error(`Could not find BlockStatement - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
|
253
253
|
};
|
|
254
254
|
|
|
255
|
-
const isDirectlyWithinTestCaseCall = (node,
|
|
255
|
+
const isDirectlyWithinTestCaseCall = (node, context) => {
|
|
256
256
|
let parent = node;
|
|
257
257
|
|
|
258
258
|
while (parent) {
|
|
@@ -260,7 +260,7 @@ const isDirectlyWithinTestCaseCall = (node, scope) => {
|
|
|
260
260
|
var _parent;
|
|
261
261
|
|
|
262
262
|
parent = parent.parent;
|
|
263
|
-
return ((_parent = parent) === null || _parent === void 0 ? void 0 : _parent.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isTypeOfJestFnCall)(parent,
|
|
263
|
+
return ((_parent = parent) === null || _parent === void 0 ? void 0 : _parent.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isTypeOfJestFnCall)(parent, context, ['test']);
|
|
264
264
|
}
|
|
265
265
|
|
|
266
266
|
parent = parent.parent;
|
|
@@ -310,7 +310,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
310
310
|
CallExpression(node) {
|
|
311
311
|
// there are too many ways that the done argument could be used with
|
|
312
312
|
// promises that contain expect that would make the promise safe for us
|
|
313
|
-
if (isTestCaseCallWithCallbackArg(node, context
|
|
313
|
+
if (isTestCaseCallWithCallbackArg(node, context)) {
|
|
314
314
|
inTestCaseWithDoneCallback = true;
|
|
315
315
|
return;
|
|
316
316
|
} // if this call expression is a promise chain, add it to the stack with
|
|
@@ -334,7 +334,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
334
334
|
// make promises containing expects safe in a test for us to be able to
|
|
335
335
|
// accurately check, so we just bail out completely if it's present
|
|
336
336
|
if (inTestCaseWithDoneCallback) {
|
|
337
|
-
if ((0, _utils2.isTypeOfJestFnCall)(node, context
|
|
337
|
+
if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['test'])) {
|
|
338
338
|
inTestCaseWithDoneCallback = false;
|
|
339
339
|
}
|
|
340
340
|
|
|
@@ -361,7 +361,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
361
361
|
// because we're most likely in the body of a function being defined
|
|
362
362
|
// within the test, which we can't track
|
|
363
363
|
|
|
364
|
-
if (!parent || !isDirectlyWithinTestCaseCall(parent, context
|
|
364
|
+
if (!parent || !isDirectlyWithinTestCaseCall(parent, context)) {
|
|
365
365
|
return;
|
|
366
366
|
}
|
|
367
367
|
|
package/lib/rules/valid-title.js
CHANGED
|
@@ -130,8 +130,7 @@ var _default = (0, _utils2.createRule)({
|
|
|
130
130
|
CallExpression(node) {
|
|
131
131
|
var _mustNotMatchPatterns, _mustMatchPatterns$je;
|
|
132
132
|
|
|
133
|
-
const
|
|
134
|
-
const jestFnCall = (0, _utils2.parseJestFnCall)(node, scope);
|
|
133
|
+
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
135
134
|
|
|
136
135
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'describe' && (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test') {
|
|
137
136
|
return;
|