eslint-plugin-jest 26.8.7 → 27.1.5

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.
Files changed (114) hide show
  1. package/README.md +82 -78
  2. package/docs/rules/consistent-test-it.md +9 -4
  3. package/docs/rules/expect-expect.md +5 -0
  4. package/docs/rules/max-expects.md +3 -1
  5. package/docs/rules/max-nested-describe.md +3 -1
  6. package/docs/rules/no-alias-methods.md +13 -2
  7. package/docs/rules/no-commented-out-tests.md +6 -1
  8. package/docs/rules/no-conditional-expect.md +7 -2
  9. package/docs/rules/no-conditional-in-test.md +3 -1
  10. package/docs/rules/no-deprecated-functions.md +14 -7
  11. package/docs/rules/no-disabled-tests.md +6 -1
  12. package/docs/rules/no-done-callback.md +9 -1
  13. package/docs/rules/no-duplicate-hooks.md +3 -1
  14. package/docs/rules/no-export.md +6 -1
  15. package/docs/rules/no-focused-tests.md +10 -1
  16. package/docs/rules/no-hooks.md +3 -1
  17. package/docs/rules/no-identical-title.md +6 -1
  18. package/docs/rules/no-if.md +4 -4
  19. package/docs/rules/no-interpolation-in-snapshots.md +6 -1
  20. package/docs/rules/no-jasmine-globals.md +10 -2
  21. package/docs/rules/no-large-snapshots.md +4 -2
  22. package/docs/rules/no-mocks-import.md +6 -1
  23. package/docs/rules/no-restricted-jest-methods.md +51 -0
  24. package/docs/rules/no-restricted-matchers.md +19 -4
  25. package/docs/rules/no-standalone-expect.md +6 -1
  26. package/docs/rules/no-test-prefixes.md +9 -1
  27. package/docs/rules/no-test-return-statement.md +2 -0
  28. package/docs/rules/prefer-called-with.md +2 -0
  29. package/docs/rules/prefer-comparison-matcher.md +5 -0
  30. package/docs/rules/prefer-each.md +56 -0
  31. package/docs/rules/prefer-equality-matcher.md +5 -0
  32. package/docs/rules/prefer-expect-assertions.md +5 -2
  33. package/docs/rules/prefer-expect-resolves.md +8 -1
  34. package/docs/rules/prefer-hooks-in-order.md +3 -1
  35. package/docs/rules/prefer-hooks-on-top.md +3 -1
  36. package/docs/rules/prefer-lowercase-title.md +5 -0
  37. package/docs/rules/prefer-mock-promise-shorthand.md +6 -1
  38. package/docs/rules/prefer-snapshot-hint.md +2 -0
  39. package/docs/rules/prefer-spy-on.md +5 -2
  40. package/docs/rules/prefer-strict-equal.md +5 -2
  41. package/docs/rules/prefer-to-be.md +8 -0
  42. package/docs/rules/prefer-to-contain.md +8 -2
  43. package/docs/rules/prefer-to-have-length.md +8 -2
  44. package/docs/rules/prefer-todo.md +5 -2
  45. package/docs/rules/require-hook.md +2 -0
  46. package/docs/rules/require-to-throw-message.md +2 -2
  47. package/docs/rules/require-top-level-describe.md +5 -1
  48. package/docs/rules/unbound-method.md +7 -2
  49. package/docs/rules/valid-describe-callback.md +6 -1
  50. package/docs/rules/valid-expect-in-promise.md +6 -1
  51. package/docs/rules/valid-expect.md +5 -2
  52. package/docs/rules/valid-title.md +9 -1
  53. package/lib/index.js +14 -25
  54. package/lib/processors/snapshot-processor.js +3 -5
  55. package/lib/rules/consistent-test-it.js +1 -19
  56. package/lib/rules/expect-expect.js +1 -18
  57. package/lib/rules/max-expects.js +0 -16
  58. package/lib/rules/max-nested-describe.js +0 -13
  59. package/lib/rules/no-alias-methods.js +1 -10
  60. package/lib/rules/no-commented-out-tests.js +0 -10
  61. package/lib/rules/no-conditional-expect.js +2 -23
  62. package/lib/rules/no-conditional-in-test.js +0 -9
  63. package/lib/rules/no-deprecated-functions.js +2 -18
  64. package/lib/rules/no-disabled-tests.js +3 -19
  65. package/lib/rules/no-done-callback.js +5 -35
  66. package/lib/rules/no-duplicate-hooks.js +0 -12
  67. package/lib/rules/no-export.js +0 -12
  68. package/lib/rules/no-focused-tests.js +1 -17
  69. package/lib/rules/no-hooks.js +0 -7
  70. package/lib/rules/no-identical-title.js +0 -19
  71. package/lib/rules/no-if.js +0 -24
  72. package/lib/rules/no-interpolation-in-snapshots.js +0 -9
  73. package/lib/rules/no-jasmine-globals.js +1 -23
  74. package/lib/rules/no-large-snapshots.js +4 -24
  75. package/lib/rules/no-mocks-import.js +0 -12
  76. package/lib/rules/no-restricted-jest-methods.js +56 -0
  77. package/lib/rules/no-restricted-matchers.js +13 -28
  78. package/lib/rules/no-standalone-expect.js +7 -33
  79. package/lib/rules/no-test-prefixes.js +1 -13
  80. package/lib/rules/no-test-return-statement.js +0 -12
  81. package/lib/rules/prefer-called-with.js +0 -10
  82. package/lib/rules/prefer-comparison-matcher.js +8 -33
  83. package/lib/rules/prefer-each.js +80 -0
  84. package/lib/rules/prefer-equality-matcher.js +12 -25
  85. package/lib/rules/prefer-expect-assertions.js +8 -54
  86. package/lib/rules/prefer-expect-resolves.js +0 -12
  87. package/lib/rules/prefer-hooks-in-order.js +2 -16
  88. package/lib/rules/prefer-hooks-on-top.js +0 -9
  89. package/lib/rules/prefer-lowercase-title.js +0 -23
  90. package/lib/rules/prefer-mock-promise-shorthand.js +5 -26
  91. package/lib/rules/prefer-snapshot-hint.js +8 -34
  92. package/lib/rules/prefer-spy-on.js +0 -17
  93. package/lib/rules/prefer-strict-equal.js +1 -11
  94. package/lib/rules/prefer-to-be.js +5 -30
  95. package/lib/rules/prefer-to-contain.js +11 -21
  96. package/lib/rules/prefer-to-have-length.js +4 -16
  97. package/lib/rules/prefer-todo.js +2 -18
  98. package/lib/rules/require-hook.js +1 -25
  99. package/lib/rules/require-to-throw-message.js +0 -9
  100. package/lib/rules/require-top-level-describe.js +1 -18
  101. package/lib/rules/unbound-method.js +3 -30
  102. package/lib/rules/utils/accessors.js +6 -18
  103. package/lib/rules/utils/detectJestVersion.js +2 -7
  104. package/lib/rules/utils/followTypeAssertionChain.js +0 -4
  105. package/lib/rules/utils/index.js +0 -10
  106. package/lib/rules/utils/misc.js +2 -46
  107. package/lib/rules/utils/parseJestFnCall.js +51 -154
  108. package/lib/rules/valid-describe-callback.js +0 -17
  109. package/lib/rules/valid-expect-in-promise.js +28 -95
  110. package/lib/rules/valid-expect.js +5 -48
  111. package/lib/rules/valid-title.js +5 -40
  112. package/package.json +20 -16
  113. package/docs/rules/no-jest-import.md +0 -20
  114. package/lib/rules/no-jest-import.js +0 -48
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.isSupportedAccessor = exports.isStringNode = exports.isIdentifier = exports.getStringValue = exports.getAccessorValue = void 0;
7
-
8
7
  var _utils = require("@typescript-eslint/utils");
9
-
10
8
  /**
11
9
  * Checks if the given `node` is a `StringLiteral`.
12
10
  *
@@ -21,7 +19,6 @@ var _utils = require("@typescript-eslint/utils");
21
19
  * @template V
22
20
  */
23
21
  const isStringLiteral = (node, value) => node.type === _utils.AST_NODE_TYPES.Literal && typeof node.value === 'string' && (value === undefined || node.value === value);
24
-
25
22
  /**
26
23
  * Checks if the given `node` is a `TemplateLiteral`.
27
24
  *
@@ -37,9 +34,9 @@ const isStringLiteral = (node, value) => node.type === _utils.AST_NODE_TYPES.Lit
37
34
  *
38
35
  * @template V
39
36
  */
40
- const isTemplateLiteral = (node, value) => node.type === _utils.AST_NODE_TYPES.TemplateLiteral && node.quasis.length === 1 && ( // bail out if not simple
37
+ const isTemplateLiteral = (node, value) => node.type === _utils.AST_NODE_TYPES.TemplateLiteral && node.quasis.length === 1 && (
38
+ // bail out if not simple
41
39
  value === undefined || node.quasis[0].value.raw === value);
42
-
43
40
  /**
44
41
  * Checks if the given `node` is a {@link StringNode}.
45
42
  *
@@ -51,6 +48,7 @@ value === undefined || node.quasis[0].value.raw === value);
51
48
  * @template V
52
49
  */
53
50
  const isStringNode = (node, specifics) => isStringLiteral(node, specifics) || isTemplateLiteral(node, specifics);
51
+
54
52
  /**
55
53
  * Gets the value of the given `StringNode`.
56
54
  *
@@ -63,18 +61,13 @@ const isStringNode = (node, specifics) => isStringLiteral(node, specifics) || is
63
61
  *
64
62
  * @template S
65
63
  */
66
-
67
-
68
64
  exports.isStringNode = isStringNode;
69
-
70
65
  const getStringValue = node => isTemplateLiteral(node) ? node.quasis[0].value.raw : node.value;
66
+
71
67
  /**
72
68
  * An `Identifier` with a known `name` value - i.e `expect`.
73
69
  */
74
-
75
-
76
70
  exports.getStringValue = getStringValue;
77
-
78
71
  /**
79
72
  * Checks if the given `node` is an `Identifier`.
80
73
  *
@@ -89,6 +82,7 @@ exports.getStringValue = getStringValue;
89
82
  * @template V
90
83
  */
91
84
  const isIdentifier = (node, name) => node.type === _utils.AST_NODE_TYPES.Identifier && (name === undefined || node.name === name);
85
+
92
86
  /**
93
87
  * Checks if the given `node` is a "supported accessor".
94
88
  *
@@ -111,11 +105,9 @@ const isIdentifier = (node, name) => node.type === _utils.AST_NODE_TYPES.Identif
111
105
  *
112
106
  * @template V
113
107
  */
114
-
115
-
116
108
  exports.isIdentifier = isIdentifier;
117
-
118
109
  const isSupportedAccessor = (node, value) => isIdentifier(node, value) || isStringNode(node, value);
110
+
119
111
  /**
120
112
  * Gets the value of the given `AccessorNode`,
121
113
  * account for the different node types.
@@ -126,10 +118,6 @@ const isSupportedAccessor = (node, value) => isIdentifier(node, value) || isStri
126
118
  *
127
119
  * @template S
128
120
  */
129
-
130
-
131
121
  exports.isSupportedAccessor = isSupportedAccessor;
132
-
133
122
  const getAccessorValue = accessor => accessor.type === _utils.AST_NODE_TYPES.Identifier ? accessor.name : getStringValue(accessor);
134
-
135
123
  exports.getAccessorValue = getAccessorValue;
@@ -5,25 +5,20 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.detectJestVersion = void 0;
7
7
  let cachedJestVersion = null;
8
-
9
8
  const detectJestVersion = () => {
10
9
  if (cachedJestVersion) {
11
10
  return cachedJestVersion;
12
11
  }
13
-
14
12
  try {
15
13
  const jestPath = require.resolve('jest/package.json');
16
-
17
- const jestPackageJson = // eslint-disable-next-line @typescript-eslint/no-require-imports
14
+ const jestPackageJson =
15
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
18
16
  require(jestPath);
19
-
20
17
  if (jestPackageJson.version) {
21
18
  const [majorVersion] = jestPackageJson.version.split('.');
22
19
  return cachedJestVersion = parseInt(majorVersion, 10);
23
20
  }
24
21
  } catch {}
25
-
26
22
  throw new Error('Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly');
27
23
  };
28
-
29
24
  exports.detectJestVersion = detectJestVersion;
@@ -4,11 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.followTypeAssertionChain = void 0;
7
-
8
7
  var _utils = require("@typescript-eslint/utils");
9
-
10
8
  const isTypeCastExpression = node => node.type === _utils.AST_NODE_TYPES.TSAsExpression || node.type === _utils.AST_NODE_TYPES.TSTypeAssertion;
11
-
12
9
  const followTypeAssertionChain = expression => isTypeCastExpression(expression) ? followTypeAssertionChain(expression.expression) : expression;
13
-
14
10
  exports.followTypeAssertionChain = followTypeAssertionChain;
@@ -3,9 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
-
7
6
  var _accessors = require("./accessors");
8
-
9
7
  Object.keys(_accessors).forEach(function (key) {
10
8
  if (key === "default" || key === "__esModule") return;
11
9
  if (key in exports && exports[key] === _accessors[key]) return;
@@ -16,9 +14,7 @@ Object.keys(_accessors).forEach(function (key) {
16
14
  }
17
15
  });
18
16
  });
19
-
20
17
  var _detectJestVersion = require("./detectJestVersion");
21
-
22
18
  Object.keys(_detectJestVersion).forEach(function (key) {
23
19
  if (key === "default" || key === "__esModule") return;
24
20
  if (key in exports && exports[key] === _detectJestVersion[key]) return;
@@ -29,9 +25,7 @@ Object.keys(_detectJestVersion).forEach(function (key) {
29
25
  }
30
26
  });
31
27
  });
32
-
33
28
  var _followTypeAssertionChain = require("./followTypeAssertionChain");
34
-
35
29
  Object.keys(_followTypeAssertionChain).forEach(function (key) {
36
30
  if (key === "default" || key === "__esModule") return;
37
31
  if (key in exports && exports[key] === _followTypeAssertionChain[key]) return;
@@ -42,9 +36,7 @@ Object.keys(_followTypeAssertionChain).forEach(function (key) {
42
36
  }
43
37
  });
44
38
  });
45
-
46
39
  var _misc = require("./misc");
47
-
48
40
  Object.keys(_misc).forEach(function (key) {
49
41
  if (key === "default" || key === "__esModule") return;
50
42
  if (key in exports && exports[key] === _misc[key]) return;
@@ -55,9 +47,7 @@ Object.keys(_misc).forEach(function (key) {
55
47
  }
56
48
  });
57
49
  });
58
-
59
50
  var _parseJestFnCall = require("./parseJestFnCall");
60
-
61
51
  Object.keys(_parseJestFnCall).forEach(function (key) {
62
52
  if (key === "default" || key === "__esModule") return;
63
53
  if (key in exports && exports[key] === _parseJestFnCall[key]) return;
@@ -6,32 +6,22 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.getFirstMatcherArg = exports.findTopMostCallExpression = exports.createRule = exports.TestCaseName = exports.ModifierName = exports.HookName = exports.EqualityMatcher = exports.DescribeAlias = void 0;
7
7
  exports.getNodeName = getNodeName;
8
8
  exports.replaceAccessorFixer = exports.isFunction = exports.isBooleanLiteral = exports.hasOnlyOneArgument = exports.getTestCallExpressionsFromDeclaredVariables = void 0;
9
-
10
9
  var _path = require("path");
11
-
12
10
  var _utils = require("@typescript-eslint/utils");
13
-
14
11
  var _package = require("../../../package.json");
15
-
16
12
  var _accessors = require("./accessors");
17
-
18
13
  var _followTypeAssertionChain = require("./followTypeAssertionChain");
19
-
20
14
  var _parseJestFnCall = require("./parseJestFnCall");
21
-
22
15
  const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
23
-
24
16
  const createRule = _utils.ESLintUtils.RuleCreator(name => {
25
17
  const ruleName = (0, _path.parse)(name).name;
26
18
  return `${REPO_URL}/blob/v${_package.version}/docs/rules/${ruleName}.md`;
27
19
  });
20
+
28
21
  /**
29
22
  * Represents a `MemberExpression` with a "known" `property`.
30
23
  */
31
-
32
-
33
24
  exports.createRule = createRule;
34
-
35
25
  /**
36
26
  * Guards that the given `call` has only one `argument`.
37
27
  *
@@ -40,20 +30,16 @@ exports.createRule = createRule;
40
30
  * @return {call is CallExpressionWithSingleArgument}
41
31
  */
42
32
  const hasOnlyOneArgument = call => call.arguments.length === 1;
43
-
44
33
  exports.hasOnlyOneArgument = hasOnlyOneArgument;
45
34
  let DescribeAlias;
46
35
  exports.DescribeAlias = DescribeAlias;
47
-
48
36
  (function (DescribeAlias) {
49
37
  DescribeAlias["describe"] = "describe";
50
38
  DescribeAlias["fdescribe"] = "fdescribe";
51
39
  DescribeAlias["xdescribe"] = "xdescribe";
52
40
  })(DescribeAlias || (exports.DescribeAlias = DescribeAlias = {}));
53
-
54
41
  let TestCaseName;
55
42
  exports.TestCaseName = TestCaseName;
56
-
57
43
  (function (TestCaseName) {
58
44
  TestCaseName["fit"] = "fit";
59
45
  TestCaseName["it"] = "it";
@@ -61,61 +47,46 @@ exports.TestCaseName = TestCaseName;
61
47
  TestCaseName["xit"] = "xit";
62
48
  TestCaseName["xtest"] = "xtest";
63
49
  })(TestCaseName || (exports.TestCaseName = TestCaseName = {}));
64
-
65
50
  let HookName;
66
51
  exports.HookName = HookName;
67
-
68
52
  (function (HookName) {
69
53
  HookName["beforeAll"] = "beforeAll";
70
54
  HookName["beforeEach"] = "beforeEach";
71
55
  HookName["afterAll"] = "afterAll";
72
56
  HookName["afterEach"] = "afterEach";
73
57
  })(HookName || (exports.HookName = HookName = {}));
74
-
75
58
  let ModifierName;
76
59
  exports.ModifierName = ModifierName;
77
-
78
60
  (function (ModifierName) {
79
61
  ModifierName["not"] = "not";
80
62
  ModifierName["rejects"] = "rejects";
81
63
  ModifierName["resolves"] = "resolves";
82
64
  })(ModifierName || (exports.ModifierName = ModifierName = {}));
83
-
84
65
  let EqualityMatcher;
85
66
  exports.EqualityMatcher = EqualityMatcher;
86
-
87
67
  (function (EqualityMatcher) {
88
68
  EqualityMatcher["toBe"] = "toBe";
89
69
  EqualityMatcher["toEqual"] = "toEqual";
90
70
  EqualityMatcher["toStrictEqual"] = "toStrictEqual";
91
71
  })(EqualityMatcher || (exports.EqualityMatcher = EqualityMatcher = {}));
92
-
93
72
  const joinNames = (a, b) => a && b ? `${a}.${b}` : null;
94
-
95
73
  function getNodeName(node) {
96
74
  if ((0, _accessors.isSupportedAccessor)(node)) {
97
75
  return (0, _accessors.getAccessorValue)(node);
98
76
  }
99
-
100
77
  switch (node.type) {
101
78
  case _utils.AST_NODE_TYPES.TaggedTemplateExpression:
102
79
  return getNodeName(node.tag);
103
-
104
80
  case _utils.AST_NODE_TYPES.MemberExpression:
105
81
  return joinNames(getNodeName(node.object), getNodeName(node.property));
106
-
107
82
  case _utils.AST_NODE_TYPES.NewExpression:
108
83
  case _utils.AST_NODE_TYPES.CallExpression:
109
84
  return getNodeName(node.callee);
110
85
  }
111
-
112
86
  return null;
113
87
  }
114
-
115
88
  const isFunction = node => node.type === _utils.AST_NODE_TYPES.FunctionExpression || node.type === _utils.AST_NODE_TYPES.ArrowFunctionExpression;
116
-
117
89
  exports.isFunction = isFunction;
118
-
119
90
  const getTestCallExpressionsFromDeclaredVariables = (declaredVariables, context) => {
120
91
  return declaredVariables.reduce((acc, {
121
92
  references
@@ -123,59 +94,44 @@ const getTestCallExpressionsFromDeclaredVariables = (declaredVariables, context)
123
94
  identifier
124
95
  }) => identifier.parent).filter(node => (node === null || node === void 0 ? void 0 : node.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _parseJestFnCall.isTypeOfJestFnCall)(node, context, ['test']))), []);
125
96
  };
97
+
126
98
  /**
127
99
  * Replaces an accessor node with the given `text`, surrounding it in quotes if required.
128
100
  *
129
101
  * This ensures that fixes produce valid code when replacing both dot-based and
130
102
  * bracket-based property accessors.
131
103
  */
132
-
133
-
134
104
  exports.getTestCallExpressionsFromDeclaredVariables = getTestCallExpressionsFromDeclaredVariables;
135
-
136
105
  const replaceAccessorFixer = (fixer, node, text) => {
137
106
  return fixer.replaceText(node, node.type === _utils.AST_NODE_TYPES.Identifier ? text : `'${text}'`);
138
107
  };
139
-
140
108
  exports.replaceAccessorFixer = replaceAccessorFixer;
141
-
142
109
  const findTopMostCallExpression = node => {
143
110
  let topMostCallExpression = node;
144
111
  let {
145
112
  parent
146
113
  } = node;
147
-
148
114
  while (parent) {
149
115
  if (parent.type === _utils.AST_NODE_TYPES.CallExpression) {
150
116
  topMostCallExpression = parent;
151
117
  parent = parent.parent;
152
118
  continue;
153
119
  }
154
-
155
120
  if (parent.type !== _utils.AST_NODE_TYPES.MemberExpression) {
156
121
  break;
157
122
  }
158
-
159
123
  parent = parent.parent;
160
124
  }
161
-
162
125
  return topMostCallExpression;
163
126
  };
164
-
165
127
  exports.findTopMostCallExpression = findTopMostCallExpression;
166
-
167
128
  const isBooleanLiteral = node => node.type === _utils.AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
168
-
169
129
  exports.isBooleanLiteral = isBooleanLiteral;
170
-
171
130
  const getFirstMatcherArg = expectFnCall => {
172
131
  const [firstArg] = expectFnCall.args;
173
-
174
132
  if (firstArg.type === _utils.AST_NODE_TYPES.SpreadElement) {
175
133
  return firstArg;
176
134
  }
177
-
178
135
  return (0, _followTypeAssertionChain.followTypeAssertionChain)(firstArg);
179
136
  };
180
-
181
137
  exports.getFirstMatcherArg = getFirstMatcherArg;