eslint-plugin-jest 26.2.2 → 26.4.1

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.
@@ -30,10 +30,17 @@ var _default = (0, _utils2.createRule)({
30
30
  return {
31
31
  CallExpression(node) {
32
32
  const scope = context.getScope();
33
- const nodeName = (0, _utils2.getNodeName)(node.callee);
34
- if (!nodeName || !(0, _utils2.isDescribeCall)(node, scope) && !(0, _utils2.isTestCaseCall)(node, scope)) return;
35
- const preferredNodeName = getPreferredNodeName(nodeName);
36
- if (!preferredNodeName) return;
33
+ const jestFnCall = (0, _utils2.parseJestFnCall)(node, scope);
34
+
35
+ if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'describe' && (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test') {
36
+ return;
37
+ }
38
+
39
+ if (jestFnCall.name[0] !== 'f' && jestFnCall.name[0] !== 'x') {
40
+ return;
41
+ }
42
+
43
+ const preferredNodeName = [jestFnCall.name.slice(1), jestFnCall.name[0] === 'f' ? 'only' : 'skip', ...jestFnCall.members.map(s => (0, _utils2.getAccessorValue)(s))].join('.');
37
44
  const funcNode = node.callee.type === _utils.AST_NODE_TYPES.TaggedTemplateExpression ? node.callee.tag : node.callee.type === _utils.AST_NODE_TYPES.CallExpression ? node.callee.callee : node.callee;
38
45
  context.report({
39
46
  messageId: 'usePreferredName',
@@ -54,19 +61,4 @@ var _default = (0, _utils2.createRule)({
54
61
 
55
62
  });
56
63
 
57
- exports.default = _default;
58
-
59
- function getPreferredNodeName(nodeName) {
60
- const firstChar = nodeName.charAt(0);
61
- const suffix = nodeName.endsWith('.each') ? '.each' : '';
62
-
63
- if (firstChar === 'f') {
64
- return `${nodeName.slice(1).replace('.each', '')}.only${suffix}`;
65
- }
66
-
67
- if (firstChar === 'x') {
68
- return `${nodeName.slice(1).replace('.each', '')}.skip${suffix}`;
69
- }
70
-
71
- return null;
72
- }
64
+ exports.default = _default;
@@ -38,7 +38,10 @@ var _default = (0, _utils2.createRule)({
38
38
  create(context) {
39
39
  return {
40
40
  CallExpression(node) {
41
- if (!(0, _utils2.isTestCaseCall)(node, context.getScope())) return;
41
+ if (!(0, _utils2.isTypeOfJestFnCall)(node, context.getScope(), ['test'])) {
42
+ return;
43
+ }
44
+
42
45
  const body = getBody(node.arguments);
43
46
  const returnStmt = body.find(t => t.type === _utils.AST_NODE_TYPES.ReturnStatement);
44
47
  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.isTestCaseCall)(node, context.getScope())) {
119
+ if ((0, _utils2.isTypeOfJestFnCall)(node, context.getScope(), ['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.isTestCaseCall)(node, context.getScope())) {
136
+ if (!(0, _utils2.isTypeOfJestFnCall)(node, context.getScope(), ['test'])) {
137
137
  return;
138
138
  }
139
139
 
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _utils = require("./utils");
9
+
10
+ const HooksOrder = ['beforeAll', 'beforeEach', 'afterEach', 'afterAll'];
11
+
12
+ var _default = (0, _utils.createRule)({
13
+ name: __filename,
14
+ meta: {
15
+ docs: {
16
+ category: 'Best Practices',
17
+ description: 'Prefer having hooks in a consistent order',
18
+ recommended: false
19
+ },
20
+ messages: {
21
+ reorderHooks: `\`{{ currentHook }}\` hooks should be before any \`{{ previousHook }}\` hooks`
22
+ },
23
+ schema: [],
24
+ type: 'suggestion'
25
+ },
26
+ defaultOptions: [],
27
+
28
+ create(context) {
29
+ let previousHookIndex = -1;
30
+ let inHook = false;
31
+ return {
32
+ CallExpression(node) {
33
+ if (inHook) {
34
+ // Ignore everything that is passed into a hook
35
+ return;
36
+ }
37
+
38
+ const jestFnCall = (0, _utils.parseJestFnCall)(node, context.getScope());
39
+
40
+ if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'hook') {
41
+ // Reset the previousHookIndex when encountering something different from a hook
42
+ previousHookIndex = -1;
43
+ return;
44
+ }
45
+
46
+ inHook = true;
47
+ const currentHook = jestFnCall.name;
48
+ const currentHookIndex = HooksOrder.indexOf(currentHook);
49
+
50
+ if (currentHookIndex < previousHookIndex) {
51
+ context.report({
52
+ messageId: 'reorderHooks',
53
+ node,
54
+ data: {
55
+ previousHook: HooksOrder[previousHookIndex],
56
+ currentHook
57
+ }
58
+ });
59
+ return;
60
+ }
61
+
62
+ previousHookIndex = currentHookIndex;
63
+ },
64
+
65
+ 'CallExpression:exit'(node) {
66
+ if ((0, _utils.isTypeOfJestFnCall)(node, context.getScope(), ['hook'])) {
67
+ inHook = false;
68
+ return;
69
+ }
70
+
71
+ if (inHook) {
72
+ return;
73
+ } // Reset the previousHookIndex when encountering something different from a hook
74
+
75
+
76
+ previousHookIndex = -1;
77
+ }
78
+
79
+ };
80
+ }
81
+
82
+ });
83
+
84
+ exports.default = _default;
@@ -29,11 +29,11 @@ var _default = (0, _utils.createRule)({
29
29
  CallExpression(node) {
30
30
  const scope = context.getScope();
31
31
 
32
- if (!(0, _utils.isHookCall)(node, scope) && (0, _utils.isTestCaseCall)(node, scope)) {
32
+ if ((0, _utils.isTypeOfJestFnCall)(node, scope, ['test'])) {
33
33
  hooksContext[hooksContext.length - 1] = true;
34
34
  }
35
35
 
36
- if (hooksContext[hooksContext.length - 1] && (0, _utils.isHookCall)(node, scope)) {
36
+ if (hooksContext[hooksContext.length - 1] && (0, _utils.isTypeOfJestFnCall)(node, scope, ['hook'])) {
37
37
  context.report({
38
38
  messageId: 'noHookOnTop',
39
39
  node
@@ -9,18 +9,6 @@ var _utils = require("./utils");
9
9
 
10
10
  const hasStringAsFirstArgument = node => node.arguments[0] && (0, _utils.isStringNode)(node.arguments[0]);
11
11
 
12
- const findNodeNameAndArgument = (node, scope) => {
13
- if (!((0, _utils.isTestCaseCall)(node, scope) || (0, _utils.isDescribeCall)(node, scope))) {
14
- return null;
15
- }
16
-
17
- if (!hasStringAsFirstArgument(node)) {
18
- return null;
19
- }
20
-
21
- return [(0, _utils.getNodeName)(node).split('.')[0], node.arguments[0]];
22
- };
23
-
24
12
  const populateIgnores = ignore => {
25
13
  const ignores = [];
26
14
 
@@ -93,22 +81,23 @@ var _default = (0, _utils.createRule)({
93
81
  return {
94
82
  CallExpression(node) {
95
83
  const scope = context.getScope();
84
+ const jestFnCall = (0, _utils.parseJestFnCall)(node, scope);
96
85
 
97
- if ((0, _utils.isDescribeCall)(node, scope)) {
86
+ if (!jestFnCall || !hasStringAsFirstArgument(node)) {
87
+ return;
88
+ }
89
+
90
+ if (jestFnCall.type === 'describe') {
98
91
  numberOfDescribeBlocks++;
99
92
 
100
93
  if (ignoreTopLevelDescribe && numberOfDescribeBlocks === 1) {
101
94
  return;
102
95
  }
103
- }
104
-
105
- const results = findNodeNameAndArgument(node, scope);
106
-
107
- if (!results) {
96
+ } else if (jestFnCall.type !== 'test') {
108
97
  return;
109
98
  }
110
99
 
111
- const [name, firstArg] = results;
100
+ const [firstArg] = node.arguments;
112
101
  const description = (0, _utils.getStringValue)(firstArg);
113
102
 
114
103
  if (allowedPrefixes.some(name => description.startsWith(name))) {
@@ -117,7 +106,7 @@ var _default = (0, _utils.createRule)({
117
106
 
118
107
  const firstCharacter = description.charAt(0);
119
108
 
120
- if (!firstCharacter || firstCharacter === firstCharacter.toLowerCase() || ignores.includes(name)) {
109
+ if (!firstCharacter || firstCharacter === firstCharacter.toLowerCase() || ignores.includes(jestFnCall.name)) {
121
110
  return;
122
111
  }
123
112
 
@@ -125,7 +114,7 @@ var _default = (0, _utils.createRule)({
125
114
  messageId: 'unexpectedLowercase',
126
115
  node: node.arguments[0],
127
116
  data: {
128
- method: name
117
+ method: jestFnCall.name
129
118
  },
130
119
 
131
120
  fix(fixer) {
@@ -139,7 +128,7 @@ var _default = (0, _utils.createRule)({
139
128
  },
140
129
 
141
130
  'CallExpression:exit'(node) {
142
- if ((0, _utils.isDescribeCall)(node, context.getScope())) {
131
+ if ((0, _utils.isTypeOfJestFnCall)(node, context.getScope(), ['describe'])) {
143
132
  numberOfDescribeBlocks--;
144
133
  }
145
134
  }
@@ -108,7 +108,7 @@ var _default = (0, _utils.createRule)({
108
108
  'CallExpression:exit'(node) {
109
109
  const scope = context.getScope();
110
110
 
111
- if ((0, _utils.isDescribeCall)(node, scope) || (0, _utils.isTestCaseCall)(node, scope)) {
111
+ if ((0, _utils.isTypeOfJestFnCall)(node, scope, ['describe', 'test'])) {
112
112
  var _depths$pop;
113
113
 
114
114
  /* istanbul ignore next */
@@ -119,7 +119,7 @@ var _default = (0, _utils.createRule)({
119
119
  CallExpression(node) {
120
120
  const scope = context.getScope();
121
121
 
122
- if ((0, _utils.isDescribeCall)(node, scope) || (0, _utils.isTestCaseCall)(node, scope)) {
122
+ if ((0, _utils.isTypeOfJestFnCall)(node, scope, ['describe', 'test'])) {
123
123
  depths.push(expressionDepth);
124
124
  expressionDepth = 0;
125
125
  }
@@ -17,12 +17,28 @@ function isEmptyFunction(node) {
17
17
  return node.body.type === _utils.AST_NODE_TYPES.BlockStatement && !node.body.body.length;
18
18
  }
19
19
 
20
- function createTodoFixer(node, fixer) {
21
- const testName = (0, _utils2.getNodeName)(node).split('.').shift();
22
- return fixer.replaceText(node.callee, `${testName}.todo`);
20
+ function createTodoFixer(jestFnCall, fixer) {
21
+ const fixes = [fixer.replaceText(jestFnCall.head.node, `${jestFnCall.head.local}.todo`)];
22
+
23
+ if (jestFnCall.members.length) {
24
+ fixes.unshift(fixer.removeRange([jestFnCall.head.node.range[1], jestFnCall.members[0].range[1]]));
25
+ }
26
+
27
+ return fixes;
23
28
  }
24
29
 
25
- const isTargetedTestCase = (node, scope) => (0, _utils2.isTestCaseCall)(node, scope) && [_utils2.TestCaseName.it, _utils2.TestCaseName.test, 'it.skip', 'test.skip'].includes((0, _utils2.getNodeName)(node));
30
+ const isTargetedTestCase = jestFnCall => {
31
+ if (jestFnCall.members.some(s => (0, _utils2.getAccessorValue)(s) !== 'skip')) {
32
+ return false;
33
+ } // todo: we should support this too (needs custom fixer)
34
+
35
+
36
+ if (jestFnCall.name.startsWith('x')) {
37
+ return false;
38
+ }
39
+
40
+ return !jestFnCall.name.startsWith('f');
41
+ };
26
42
 
27
43
  var _default = (0, _utils2.createRule)({
28
44
  name: __filename,
@@ -46,8 +62,9 @@ var _default = (0, _utils2.createRule)({
46
62
  return {
47
63
  CallExpression(node) {
48
64
  const [title, callback] = node.arguments;
65
+ const jestFnCall = (0, _utils2.parseJestFnCall)(node, context.getScope());
49
66
 
50
- if (!title || !isTargetedTestCase(node, context.getScope()) || !(0, _utils2.isStringNode)(title)) {
67
+ if (!title || (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test' || !isTargetedTestCase(jestFnCall) || !(0, _utils2.isStringNode)(title)) {
51
68
  return;
52
69
  }
53
70
 
@@ -55,7 +72,7 @@ var _default = (0, _utils2.createRule)({
55
72
  context.report({
56
73
  messageId: 'emptyTest',
57
74
  node,
58
- fix: fixer => [fixer.removeRange([title.range[1], callback.range[1]]), createTodoFixer(node, fixer)]
75
+ fix: fixer => [fixer.removeRange([title.range[1], callback.range[1]]), ...createTodoFixer(jestFnCall, fixer)]
59
76
  });
60
77
  }
61
78
 
@@ -63,7 +80,7 @@ var _default = (0, _utils2.createRule)({
63
80
  context.report({
64
81
  messageId: 'unimplementedTest',
65
82
  node,
66
- fix: fixer => [createTodoFixer(node, fixer)]
83
+ fix: fixer => createTodoFixer(jestFnCall, fixer)
67
84
  });
68
85
  }
69
86
  }
@@ -12,7 +12,7 @@ var _utils2 = require("./utils");
12
12
  const isJestFnCall = (node, scope) => {
13
13
  var _getNodeName;
14
14
 
15
- if ((0, _utils2.isDescribeCall)(node, scope) || (0, _utils2.isTestCaseCall)(node, scope) || (0, _utils2.isHookCall)(node, scope)) {
15
+ if ((0, _utils2.parseJestFnCall)(node, scope)) {
16
16
  return true;
17
17
  }
18
18
 
@@ -100,7 +100,7 @@ var _default = (0, _utils2.createRule)({
100
100
  },
101
101
 
102
102
  CallExpression(node) {
103
- if (!(0, _utils2.isDescribeCall)(node, context.getScope()) || node.arguments.length < 2) {
103
+ if (!(0, _utils2.isTypeOfJestFnCall)(node, context.getScope(), ['describe']) || node.arguments.length < 2) {
104
104
  return;
105
105
  }
106
106
 
@@ -47,8 +47,13 @@ var _default = (0, _utils.createRule)({
47
47
  return {
48
48
  CallExpression(node) {
49
49
  const scope = context.getScope();
50
+ const jestFnCall = (0, _utils.parseJestFnCall)(node, scope);
50
51
 
51
- if ((0, _utils.isDescribeCall)(node, scope)) {
52
+ if (!jestFnCall) {
53
+ return;
54
+ }
55
+
56
+ if (jestFnCall.type === 'describe') {
52
57
  numberOfDescribeBlocks++;
53
58
 
54
59
  if (numberOfDescribeBlocks === 1) {
@@ -70,7 +75,7 @@ var _default = (0, _utils.createRule)({
70
75
  }
71
76
 
72
77
  if (numberOfDescribeBlocks === 0) {
73
- if ((0, _utils.isTestCaseCall)(node, scope)) {
78
+ if (jestFnCall.type === 'test') {
74
79
  context.report({
75
80
  node,
76
81
  messageId: 'unexpectedTestCase'
@@ -78,7 +83,7 @@ var _default = (0, _utils.createRule)({
78
83
  return;
79
84
  }
80
85
 
81
- if ((0, _utils.isHookCall)(node, scope)) {
86
+ if (jestFnCall.type === 'hook') {
82
87
  context.report({
83
88
  node,
84
89
  messageId: 'unexpectedHook'
@@ -89,7 +94,7 @@ var _default = (0, _utils.createRule)({
89
94
  },
90
95
 
91
96
  'CallExpression:exit'(node) {
92
- if ((0, _utils.isDescribeCall)(node, context.getScope())) {
97
+ if ((0, _utils.isTypeOfJestFnCall)(node, context.getScope(), ['describe'])) {
93
98
  numberOfDescribeBlocks--;
94
99
  }
95
100
  }