eslint-plugin-jest 28.2.0 → 28.4.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 CHANGED
@@ -363,7 +363,7 @@ set to warn in.\
363
363
  | [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | | | |
364
364
  | [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | | | |
365
365
  | [valid-describe-callback](docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback | ✅ | | | |
366
- | [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ✅ | | | |
366
+ | [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ✅ | | 🔧 | |
367
367
  | [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Require promises that have expectations in their chain to be valid | ✅ | | | |
368
368
  | [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ✅ | | 🔧 | |
369
369
 
@@ -42,6 +42,42 @@ describe('foo', () => {
42
42
  });
43
43
  ```
44
44
 
45
+ ## Options
46
+
47
+ This rule can be configured as follows
48
+
49
+ ```json
50
+ {
51
+ "type": "object",
52
+ "properties": {
53
+ "types": {
54
+ "type": "array",
55
+ "items": {
56
+ "type": "string",
57
+ "enum": ["hook", "describe", "test", "expect", "jest", "unknown"]
58
+ }
59
+ }
60
+ },
61
+ "additionalProperties": false
62
+ }
63
+ ```
64
+
65
+ #### types
66
+
67
+ A list of Jest global types to enforce explicit imports for. By default, all
68
+ Jest globals are enforced.
69
+
70
+ This option is useful when you only want to enforce explicit imports for a
71
+ subset of Jest globals. For instance, when migrating to ESM, you might want to
72
+ enforce explicit imports only for the `jest` global, as of
73
+ [Jest's ESM documentation](https://jestjs.io/docs/ecmascript-modules#differences-between-esm-and-commonjs).
74
+
75
+ ```json5
76
+ {
77
+ 'jest/prefer-importing-jest-globals': ['error', { types: ['jest'] }],
78
+ }
79
+ ```
80
+
45
81
  ## Further Reading
46
82
 
47
83
  - [Documentation](https://jestjs.io/docs/api)
@@ -3,8 +3,14 @@
3
3
  💼 This rule is enabled in the ✅ `recommended`
4
4
  [config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
5
5
 
6
+ 🔧 This rule is automatically fixable by the
7
+ [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
8
+
6
9
  <!-- end auto-generated rule header -->
7
10
 
11
+ > [!NOTE] Test function will be fixed if it is async and does not have await in
12
+ > the async assertion.
13
+
8
14
  Ensure `expect()` is called with a single argument and there is an actual
9
15
  expectation made.
10
16
 
@@ -40,9 +40,9 @@ var _default = exports.default = (0, _utils2.createRule)({
40
40
  properties: {
41
41
  assertFunctionNames: {
42
42
  type: 'array',
43
- items: [{
43
+ items: {
44
44
  type: 'string'
45
- }]
45
+ }
46
46
  },
47
47
  additionalTestBlockFunctions: {
48
48
  type: 'array',
@@ -10,6 +10,7 @@ const createFixerImports = (isModule, functionsToImport) => {
10
10
  const allImportsFormatted = Array.from(functionsToImport).sort().join(', ');
11
11
  return isModule ? `import { ${allImportsFormatted} } from '@jest/globals';` : `const { ${allImportsFormatted} } = require('@jest/globals');`;
12
12
  };
13
+ const allJestFnTypes = ['hook', 'describe', 'test', 'expect', 'jest', 'unknown'];
13
14
  var _default = exports.default = (0, _utils2.createRule)({
14
15
  name: __filename,
15
16
  meta: {
@@ -21,17 +22,34 @@ var _default = exports.default = (0, _utils2.createRule)({
21
22
  },
22
23
  fixable: 'code',
23
24
  type: 'problem',
24
- schema: []
25
+ schema: [{
26
+ type: 'object',
27
+ properties: {
28
+ types: {
29
+ type: 'array',
30
+ items: {
31
+ type: 'string',
32
+ enum: allJestFnTypes
33
+ }
34
+ }
35
+ },
36
+ additionalProperties: false
37
+ }]
25
38
  },
26
- defaultOptions: [],
39
+ defaultOptions: [{
40
+ types: allJestFnTypes
41
+ }],
27
42
  create(context) {
43
+ const {
44
+ types = allJestFnTypes
45
+ } = context.options[0] || {};
28
46
  const importedFunctionsWithSource = {};
29
47
  const functionsToImport = new Set();
30
48
  let reportingNode;
31
49
  return {
32
50
  ImportDeclaration(node) {
33
51
  node.specifiers.forEach(specifier => {
34
- if (specifier.type === 'ImportSpecifier') {
52
+ if (specifier.type === _utils.AST_NODE_TYPES.ImportSpecifier) {
35
53
  importedFunctionsWithSource[specifier.local.name] = node.source.value;
36
54
  }
37
55
  });
@@ -41,7 +59,7 @@ var _default = exports.default = (0, _utils2.createRule)({
41
59
  if (!jestFnCall) {
42
60
  return;
43
61
  }
44
- if (jestFnCall.head.type !== 'import') {
62
+ if (jestFnCall.head.type !== 'import' && types.includes(jestFnCall.type)) {
45
63
  functionsToImport.add(jestFnCall.name);
46
64
  reportingNode ||= jestFnCall.head.node;
47
65
  }
@@ -28,6 +28,14 @@ const getPromiseCallExpressionNode = node => {
28
28
  return null;
29
29
  };
30
30
  const findPromiseCallExpressionNode = node => node.parent?.parent && [_utils.AST_NODE_TYPES.CallExpression, _utils.AST_NODE_TYPES.ArrayExpression].includes(node.parent.type) ? getPromiseCallExpressionNode(node.parent) : null;
31
+ const findFirstAsyncFunction = ({
32
+ parent
33
+ }) => {
34
+ if (!parent) {
35
+ return null;
36
+ }
37
+ return (0, _utils2.isFunction)(parent) && parent.async ? parent : findFirstAsyncFunction(parent);
38
+ };
31
39
  const getParentIfThenified = node => {
32
40
  const grandParentNode = node.parent?.parent;
33
41
  if (grandParentNode && grandParentNode.type === _utils.AST_NODE_TYPES.CallExpression && grandParentNode.callee.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(grandParentNode.callee.property) && ['then', 'catch'].includes((0, _utils2.getAccessorValue)(grandParentNode.callee.property)) && grandParentNode.parent) {
@@ -65,6 +73,7 @@ var _default = exports.default = (0, _utils2.createRule)({
65
73
  asyncMustBeAwaited: 'Async assertions must be awaited{{ orReturned }}',
66
74
  promisesWithAsyncAssertionsMustBeAwaited: 'Promises which return async assertions must be awaited{{ orReturned }}'
67
75
  },
76
+ fixable: 'code',
68
77
  type: 'suggestion',
69
78
  schema: [{
70
79
  type: 'object',
@@ -242,7 +251,19 @@ var _default = exports.default = (0, _utils2.createRule)({
242
251
  orReturned
243
252
  },
244
253
  messageId: finalNode === targetNode ? 'asyncMustBeAwaited' : 'promisesWithAsyncAssertionsMustBeAwaited',
245
- node
254
+ node,
255
+ fix(fixer) {
256
+ if (!findFirstAsyncFunction(finalNode)) {
257
+ return [];
258
+ }
259
+ const returnStatement = finalNode.parent?.type === _utils.AST_NODE_TYPES.ReturnStatement ? finalNode.parent : null;
260
+ if (alwaysAwait && returnStatement) {
261
+ const sourceCodeText = (0, _utils2.getSourceCode)(context).getText(returnStatement);
262
+ const replacedText = sourceCodeText.replace('return', 'await');
263
+ return fixer.replaceText(returnStatement, replacedText);
264
+ }
265
+ return fixer.insertTextBefore(finalNode, 'await ');
266
+ }
246
267
  });
247
268
  if (isParentArrayExpression) {
248
269
  pushPromiseArrayException(finalNode.loc);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "28.2.0",
3
+ "version": "28.4.0",
4
4
  "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",
@@ -29,7 +29,7 @@
29
29
  "prettier:write": "prettier --write 'docs/**/*.md' README.md '.github/**' package.json tsconfig.json src/globals.json .yarnrc.yml",
30
30
  "postpublish": "pinst --enable",
31
31
  "test": "jest",
32
- "tools:regenerate-docs": "yarn prepack && eslint-doc-generator",
32
+ "regenerate-docs": "yarn prepack && eslint-doc-generator",
33
33
  "typecheck": "tsc -p ."
34
34
  },
35
35
  "commitlint": {
@@ -86,13 +86,13 @@
86
86
  "babel-jest": "^29.0.0",
87
87
  "babel-plugin-replace-ts-export-assignment": "^0.0.2",
88
88
  "dedent": "^1.5.0",
89
- "eslint": "^7.0.0 || ^8.0.0 || ^9.0.0",
89
+ "eslint": "^7.0.0 || ^8.0.0",
90
90
  "eslint-config-prettier": "^9.0.0",
91
91
  "eslint-doc-generator": "^1.0.0",
92
92
  "eslint-plugin-eslint-comments": "^3.1.2",
93
- "eslint-plugin-eslint-plugin": "^5.0.6",
93
+ "eslint-plugin-eslint-plugin": "^6.0.0",
94
94
  "eslint-plugin-import": "^2.25.1",
95
- "eslint-plugin-n": "^15.0.0",
95
+ "eslint-plugin-n": "^17.0.0",
96
96
  "eslint-plugin-prettier": "^5.0.0",
97
97
  "eslint-remote-tester": "^3.0.0",
98
98
  "eslint-remote-tester-repositories": "~1.0.0",
@@ -124,7 +124,7 @@
124
124
  "optional": true
125
125
  }
126
126
  },
127
- "packageManager": "yarn@3.8.1",
127
+ "packageManager": "yarn@3.8.2",
128
128
  "engines": {
129
129
  "node": "^16.10.0 || ^18.12.0 || >=20.0.0"
130
130
  },