eslint-plugin-jest 24.4.2 → 26.1.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.
Files changed (77) hide show
  1. package/README.md +75 -50
  2. package/docs/rules/expect-expect.md +42 -1
  3. package/docs/rules/max-nested-describe.md +4 -5
  4. package/docs/rules/no-conditional-expect.md +57 -3
  5. package/docs/rules/no-conditional-in-test.md +79 -0
  6. package/docs/rules/no-deprecated-functions.md +5 -0
  7. package/docs/rules/no-done-callback.md +3 -3
  8. package/docs/rules/no-if.md +5 -0
  9. package/docs/rules/no-standalone-expect.md +3 -3
  10. package/docs/rules/no-test-return-statement.md +1 -2
  11. package/docs/rules/prefer-comparison-matcher.md +55 -0
  12. package/docs/rules/prefer-equality-matcher.md +29 -0
  13. package/docs/rules/prefer-expect-assertions.md +126 -0
  14. package/docs/rules/prefer-expect-resolves.md +53 -0
  15. package/docs/rules/prefer-hooks-on-top.md +72 -48
  16. package/docs/rules/{lowercase-name.md → prefer-lowercase-title.md} +7 -7
  17. package/docs/rules/prefer-snapshot-hint.md +188 -0
  18. package/docs/rules/prefer-to-be.md +53 -0
  19. package/docs/rules/require-hook.md +187 -0
  20. package/docs/rules/require-top-level-describe.md +28 -0
  21. package/docs/rules/{valid-describe.md → valid-describe-callback.md} +1 -1
  22. package/docs/rules/valid-expect-in-promise.md +55 -14
  23. package/docs/rules/valid-expect.md +13 -0
  24. package/docs/rules/valid-title.md +30 -2
  25. package/lib/index.js +2 -3
  26. package/lib/processors/snapshot-processor.js +1 -1
  27. package/lib/rules/consistent-test-it.js +20 -20
  28. package/lib/rules/detectJestVersion.js +29 -0
  29. package/lib/rules/expect-expect.js +25 -11
  30. package/lib/rules/max-nested-describe.js +5 -5
  31. package/lib/rules/no-conditional-expect.js +9 -9
  32. package/lib/rules/no-conditional-in-test.js +60 -0
  33. package/lib/rules/no-deprecated-functions.js +14 -32
  34. package/lib/rules/no-done-callback.js +10 -10
  35. package/lib/rules/no-export.js +6 -6
  36. package/lib/rules/no-focused-tests.js +11 -11
  37. package/lib/rules/no-identical-title.js +3 -3
  38. package/lib/rules/no-if.js +13 -11
  39. package/lib/rules/no-interpolation-in-snapshots.js +6 -6
  40. package/lib/rules/no-jasmine-globals.js +10 -10
  41. package/lib/rules/no-large-snapshots.js +11 -11
  42. package/lib/rules/no-standalone-expect.js +14 -14
  43. package/lib/rules/no-test-prefixes.js +6 -6
  44. package/lib/rules/no-test-return-statement.js +8 -8
  45. package/lib/rules/prefer-comparison-matcher.js +139 -0
  46. package/lib/rules/prefer-equality-matcher.js +98 -0
  47. package/lib/rules/prefer-expect-assertions.js +93 -11
  48. package/lib/rules/prefer-expect-resolves.js +48 -0
  49. package/lib/rules/prefer-hooks-on-top.js +1 -1
  50. package/lib/rules/{lowercase-name.js → prefer-lowercase-title.js} +20 -1
  51. package/lib/rules/prefer-snapshot-hint.js +112 -0
  52. package/lib/rules/prefer-spy-on.js +9 -9
  53. package/lib/rules/prefer-to-be.js +136 -0
  54. package/lib/rules/prefer-to-contain.js +19 -67
  55. package/lib/rules/prefer-to-have-length.js +9 -14
  56. package/lib/rules/prefer-todo.js +9 -9
  57. package/lib/rules/require-hook.js +121 -0
  58. package/lib/rules/require-top-level-describe.js +40 -6
  59. package/lib/rules/utils.js +34 -30
  60. package/lib/rules/{valid-describe.js → valid-describe-callback.js} +9 -9
  61. package/lib/rules/valid-expect-in-promise.js +336 -67
  62. package/lib/rules/valid-expect.js +36 -19
  63. package/lib/rules/valid-title.js +61 -61
  64. package/package.json +40 -27
  65. package/CHANGELOG.md +0 -513
  66. package/docs/rules/no-expect-resolves.md +0 -47
  67. package/docs/rules/no-truthy-falsy.md +0 -53
  68. package/docs/rules/no-try-expect.md +0 -63
  69. package/docs/rules/prefer-inline-snapshots.md +0 -51
  70. package/docs/rules/prefer-to-be-null.md +0 -33
  71. package/docs/rules/prefer-to-be-undefined.md +0 -33
  72. package/lib/rules/no-expect-resolves.js +0 -40
  73. package/lib/rules/no-truthy-falsy.js +0 -58
  74. package/lib/rules/no-try-expect.js +0 -89
  75. package/lib/rules/prefer-inline-snapshots.js +0 -69
  76. package/lib/rules/prefer-to-be-null.js +0 -67
  77. package/lib/rules/prefer-to-be-undefined.js +0 -67
@@ -5,44 +5,59 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _experimentalUtils = require("@typescript-eslint/experimental-utils");
8
+ var _utils = require("@typescript-eslint/utils");
9
9
 
10
- var _utils = require("./utils");
10
+ var _utils2 = require("./utils");
11
11
 
12
12
  const trimFXprefix = word => ['f', 'x'].includes(word.charAt(0)) ? word.substr(1) : word;
13
13
 
14
14
  const doesBinaryExpressionContainStringNode = binaryExp => {
15
- if ((0, _utils.isStringNode)(binaryExp.right)) {
15
+ if ((0, _utils2.isStringNode)(binaryExp.right)) {
16
16
  return true;
17
17
  }
18
18
 
19
- if (binaryExp.left.type === _experimentalUtils.AST_NODE_TYPES.BinaryExpression) {
19
+ if (binaryExp.left.type === _utils.AST_NODE_TYPES.BinaryExpression) {
20
20
  return doesBinaryExpressionContainStringNode(binaryExp.left);
21
21
  }
22
22
 
23
- return (0, _utils.isStringNode)(binaryExp.left);
23
+ return (0, _utils2.isStringNode)(binaryExp.left);
24
24
  };
25
25
 
26
- const quoteStringValue = node => node.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral ? `\`${node.quasis[0].value.raw}\`` : node.raw;
26
+ const quoteStringValue = node => node.type === _utils.AST_NODE_TYPES.TemplateLiteral ? `\`${node.quasis[0].value.raw}\`` : node.raw;
27
+
28
+ const compileMatcherPattern = matcherMaybeWithMessage => {
29
+ const [matcher, message] = Array.isArray(matcherMaybeWithMessage) ? matcherMaybeWithMessage : [matcherMaybeWithMessage];
30
+ return [new RegExp(matcher, 'u'), message];
31
+ };
27
32
 
28
33
  const compileMatcherPatterns = matchers => {
29
- if (typeof matchers === 'string') {
30
- const matcher = new RegExp(matchers, 'u');
34
+ if (typeof matchers === 'string' || Array.isArray(matchers)) {
35
+ const compiledMatcher = compileMatcherPattern(matchers);
31
36
  return {
32
- describe: matcher,
33
- test: matcher,
34
- it: matcher
37
+ describe: compiledMatcher,
38
+ test: compiledMatcher,
39
+ it: compiledMatcher
35
40
  };
36
41
  }
37
42
 
38
43
  return {
39
- describe: matchers.describe ? new RegExp(matchers.describe, 'u') : null,
40
- test: matchers.test ? new RegExp(matchers.test, 'u') : null,
41
- it: matchers.it ? new RegExp(matchers.it, 'u') : null
44
+ describe: matchers.describe ? compileMatcherPattern(matchers.describe) : null,
45
+ test: matchers.test ? compileMatcherPattern(matchers.test) : null,
46
+ it: matchers.it ? compileMatcherPattern(matchers.it) : null
42
47
  };
43
48
  };
44
49
 
45
- var _default = (0, _utils.createRule)({
50
+ const MatcherAndMessageSchema = {
51
+ type: 'array',
52
+ items: {
53
+ type: 'string'
54
+ },
55
+ minItems: 1,
56
+ maxItems: 2,
57
+ additionalItems: false
58
+ };
59
+
60
+ var _default = (0, _utils2.createRule)({
46
61
  name: __filename,
47
62
  meta: {
48
63
  docs: {
@@ -57,7 +72,9 @@ var _default = (0, _utils.createRule)({
57
72
  accidentalSpace: 'should not have leading or trailing spaces',
58
73
  disallowedWord: '"{{ word }}" is not allowed in test titles.',
59
74
  mustNotMatch: '{{ jestFunctionName }} should not match {{ pattern }}',
60
- mustMatch: '{{ jestFunctionName }} should match {{ pattern }}'
75
+ mustMatch: '{{ jestFunctionName }} should match {{ pattern }}',
76
+ mustNotMatchCustom: '{{ message }}',
77
+ mustMatchCustom: '{{ message }}'
61
78
  },
62
79
  type: 'suggestion',
63
80
  schema: [{
@@ -72,43 +89,22 @@ var _default = (0, _utils.createRule)({
72
89
  items: {
73
90
  type: 'string'
74
91
  }
75
- },
76
- mustNotMatch: {
92
+ }
93
+ },
94
+ patternProperties: {
95
+ [/^must(?:Not)?Match$/u.source]: {
77
96
  oneOf: [{
78
97
  type: 'string'
79
- }, {
98
+ }, MatcherAndMessageSchema, {
80
99
  type: 'object',
81
- properties: {
82
- describe: {
83
- type: 'string'
84
- },
85
- test: {
86
- type: 'string'
87
- },
88
- it: {
89
- type: 'string'
90
- }
100
+ propertyNames: {
101
+ enum: ['describe', 'test', 'it']
91
102
  },
92
- additionalProperties: false
93
- }]
94
- },
95
- mustMatch: {
96
- oneOf: [{
97
- type: 'string'
98
- }, {
99
- type: 'object',
100
- properties: {
101
- describe: {
103
+ additionalProperties: {
104
+ oneOf: [{
102
105
  type: 'string'
103
- },
104
- test: {
105
- type: 'string'
106
- },
107
- it: {
108
- type: 'string'
109
- }
110
- },
111
- additionalProperties: false
106
+ }, MatcherAndMessageSchema]
107
+ }
112
108
  }]
113
109
  }
114
110
  },
@@ -132,7 +128,9 @@ var _default = (0, _utils.createRule)({
132
128
  const mustMatchPatterns = compileMatcherPatterns(mustMatch !== null && mustMatch !== void 0 ? mustMatch : {});
133
129
  return {
134
130
  CallExpression(node) {
135
- if (!(0, _utils.isDescribeCall)(node) && !(0, _utils.isTestCaseCall)(node)) {
131
+ var _mustNotMatchPatterns, _mustMatchPatterns$je;
132
+
133
+ if (!(0, _utils2.isDescribeCall)(node) && !(0, _utils2.isTestCaseCall)(node)) {
136
134
  return;
137
135
  }
138
136
 
@@ -142,12 +140,12 @@ var _default = (0, _utils.createRule)({
142
140
  return;
143
141
  }
144
142
 
145
- if (!(0, _utils.isStringNode)(argument)) {
146
- if (argument.type === _experimentalUtils.AST_NODE_TYPES.BinaryExpression && doesBinaryExpressionContainStringNode(argument)) {
143
+ if (!(0, _utils2.isStringNode)(argument)) {
144
+ if (argument.type === _utils.AST_NODE_TYPES.BinaryExpression && doesBinaryExpressionContainStringNode(argument)) {
147
145
  return;
148
146
  }
149
147
 
150
- if (argument.type !== _experimentalUtils.AST_NODE_TYPES.TemplateLiteral && !(ignoreTypeOfDescribeName && (0, _utils.isDescribeCall)(node))) {
148
+ if (argument.type !== _utils.AST_NODE_TYPES.TemplateLiteral && !(ignoreTypeOfDescribeName && (0, _utils2.isDescribeCall)(node))) {
151
149
  context.report({
152
150
  messageId: 'titleMustBeString',
153
151
  loc: argument.loc
@@ -157,13 +155,13 @@ var _default = (0, _utils.createRule)({
157
155
  return;
158
156
  }
159
157
 
160
- const title = (0, _utils.getStringValue)(argument);
158
+ const title = (0, _utils2.getStringValue)(argument);
161
159
 
162
160
  if (!title) {
163
161
  context.report({
164
162
  messageId: 'emptyTitle',
165
163
  data: {
166
- jestFunctionName: (0, _utils.isDescribeCall)(node) ? _utils.DescribeAlias.describe : _utils.TestCaseName.test
164
+ jestFunctionName: (0, _utils2.isDescribeCall)(node) ? _utils2.DescribeAlias.describe : _utils2.TestCaseName.test
167
165
  },
168
166
  node
169
167
  });
@@ -193,7 +191,7 @@ var _default = (0, _utils.createRule)({
193
191
  });
194
192
  }
195
193
 
196
- const nodeName = trimFXprefix((0, _utils.getNodeName)(node));
194
+ const nodeName = trimFXprefix((0, _utils2.getNodeName)(node));
197
195
  const [firstWord] = title.split(' ');
198
196
 
199
197
  if (firstWord.toLowerCase() === nodeName) {
@@ -205,32 +203,34 @@ var _default = (0, _utils.createRule)({
205
203
  }
206
204
 
207
205
  const [jestFunctionName] = nodeName.split('.');
208
- const mustNotMatchPattern = mustNotMatchPatterns[jestFunctionName];
206
+ const [mustNotMatchPattern, mustNotMatchMessage] = (_mustNotMatchPatterns = mustNotMatchPatterns[jestFunctionName]) !== null && _mustNotMatchPatterns !== void 0 ? _mustNotMatchPatterns : [];
209
207
 
210
208
  if (mustNotMatchPattern) {
211
209
  if (mustNotMatchPattern.test(title)) {
212
210
  context.report({
213
- messageId: 'mustNotMatch',
211
+ messageId: mustNotMatchMessage ? 'mustNotMatchCustom' : 'mustNotMatch',
214
212
  node: argument,
215
213
  data: {
216
214
  jestFunctionName,
217
- pattern: mustNotMatchPattern
215
+ pattern: mustNotMatchPattern,
216
+ message: mustNotMatchMessage
218
217
  }
219
218
  });
220
219
  return;
221
220
  }
222
221
  }
223
222
 
224
- const mustMatchPattern = mustMatchPatterns[jestFunctionName];
223
+ const [mustMatchPattern, mustMatchMessage] = (_mustMatchPatterns$je = mustMatchPatterns[jestFunctionName]) !== null && _mustMatchPatterns$je !== void 0 ? _mustMatchPatterns$je : [];
225
224
 
226
225
  if (mustMatchPattern) {
227
226
  if (!mustMatchPattern.test(title)) {
228
227
  context.report({
229
- messageId: 'mustMatch',
228
+ messageId: mustMatchMessage ? 'mustMatchCustom' : 'mustMatch',
230
229
  node: argument,
231
230
  data: {
232
231
  jestFunctionName,
233
- pattern: mustMatchPattern
232
+ pattern: mustMatchPattern,
233
+ message: mustMatchMessage
234
234
  }
235
235
  });
236
236
  return;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "24.4.2",
4
- "description": "Eslint rules for Jest",
3
+ "version": "26.1.0",
4
+ "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",
7
7
  "eslintplugin",
@@ -60,10 +60,13 @@
60
60
  "projects": [
61
61
  {
62
62
  "displayName": "test",
63
- "testEnvironment": "node",
63
+ "moduleNameMapper": {
64
+ "eslint/use-at-your-own-risk": "eslint/lib/unsupported-api.js"
65
+ },
64
66
  "testPathIgnorePatterns": [
65
67
  "<rootDir>/lib/.*",
66
- "<rootDir>/src/rules/__tests__/fixtures/*"
68
+ "<rootDir>/src/rules/__tests__/fixtures/*",
69
+ "<rootDir>/src/rules/__tests__/test-utils.ts"
67
70
  ]
68
71
  },
69
72
  {
@@ -79,60 +82,64 @@
79
82
  ]
80
83
  },
81
84
  "dependencies": {
82
- "@typescript-eslint/experimental-utils": "^4.0.1"
85
+ "@typescript-eslint/utils": "^5.10.0"
83
86
  },
84
87
  "devDependencies": {
85
88
  "@babel/cli": "^7.4.4",
86
89
  "@babel/core": "^7.4.4",
87
90
  "@babel/preset-env": "^7.4.4",
88
91
  "@babel/preset-typescript": "^7.3.3",
89
- "@commitlint/cli": "^12.0.0",
90
- "@commitlint/config-conventional": "^12.0.0",
92
+ "@commitlint/cli": "^16.0.0",
93
+ "@commitlint/config-conventional": "^16.0.0",
91
94
  "@schemastore/package": "^0.0.6",
92
- "@semantic-release/changelog": "^5.0.1",
93
- "@semantic-release/git": "^9.0.0",
95
+ "@semantic-release/changelog": "^6.0.0",
96
+ "@semantic-release/git": "^10.0.0",
94
97
  "@types/dedent": "^0.7.0",
95
98
  "@types/jest": "^27.0.0",
96
- "@types/node": "^14.0.0",
99
+ "@types/node": "^16.0.0",
97
100
  "@types/prettier": "^2.0.0",
98
- "@typescript-eslint/eslint-plugin": "^4.0.1",
99
- "@typescript-eslint/parser": "^4.0.1",
101
+ "@typescript-eslint/eslint-plugin": "^5.0.0",
102
+ "@typescript-eslint/parser": "^5.0.0",
100
103
  "babel-jest": "^27.0.0",
101
104
  "babel-plugin-replace-ts-export-assignment": "^0.0.2",
102
105
  "dedent": "^0.7.0",
103
- "eslint": "^5.1.0 || ^6.0.0 || ^7.0.0",
104
- "eslint-config-prettier": "^6.5.0",
106
+ "eslint": "^6.0.0 || ^7.0.0 || ^8.0.0",
107
+ "eslint-config-prettier": "^8.3.0",
105
108
  "eslint-plugin-eslint-comments": "^3.1.2",
106
109
  "eslint-plugin-eslint-config": "^2.0.0",
107
- "eslint-plugin-eslint-plugin": "^2.0.0",
108
- "eslint-plugin-import": "^2.20.2",
110
+ "eslint-plugin-eslint-plugin": "^4.0.1",
111
+ "eslint-plugin-import": "^2.25.1",
109
112
  "eslint-plugin-node": "^11.0.0",
110
- "eslint-plugin-prettier": "^3.0.0",
111
- "husky": "^6.0.0",
113
+ "eslint-plugin-prettier": "^3.4.1",
114
+ "eslint-remote-tester": "^2.1.0",
115
+ "eslint-remote-tester-repositories": "^0.0.4",
116
+ "husky": "^7.0.2",
112
117
  "is-ci": "^3.0.0",
113
118
  "jest": "^27.0.0",
114
- "jest-runner-eslint": "^0.10.0",
115
- "lint-staged": "^10.2.2",
119
+ "jest-runner-eslint": "^1.0.0",
120
+ "lint-staged": "^12.0.0",
116
121
  "pinst": "^2.0.0",
117
122
  "prettier": "^2.0.5",
118
- "resolve-from": "^5.0.0",
119
123
  "rimraf": "^3.0.0",
120
- "semantic-release": "^17.0.7",
124
+ "semantic-release": "^19.0.0",
121
125
  "semver": "^7.3.5",
122
- "ts-node": "^9.0.0",
126
+ "ts-node": "^10.2.1",
123
127
  "typescript": "^4.4.0"
124
128
  },
125
129
  "peerDependencies": {
126
- "@typescript-eslint/eslint-plugin": ">= 4",
127
- "eslint": ">=5"
130
+ "@typescript-eslint/eslint-plugin": "^5.0.0",
131
+ "eslint": "^6.0.0 || ^7.0.0 || ^8.0.0"
128
132
  },
129
133
  "peerDependenciesMeta": {
130
134
  "@typescript-eslint/eslint-plugin": {
131
135
  "optional": true
136
+ },
137
+ "jest": {
138
+ "optional": true
132
139
  }
133
140
  },
134
141
  "engines": {
135
- "node": ">=10"
142
+ "node": "^12.13.0 || ^14.15.0 || >=16.0.0"
136
143
  },
137
144
  "release": {
138
145
  "branches": [
@@ -150,5 +157,11 @@
150
157
  "@semantic-release/git",
151
158
  "@semantic-release/github"
152
159
  ]
153
- }
160
+ },
161
+ "resolutions": {
162
+ "@semantic-release/npm/npm": "7.20.6",
163
+ "@typescript-eslint/experimental-utils": "^5.0.0",
164
+ "fsevents/node-gyp": "^7.0.0"
165
+ },
166
+ "packageManager": "yarn@3.1.1"
154
167
  }