eslint-plugin-jest 29.1.0 → 29.2.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.
package/README.md CHANGED
@@ -386,6 +386,7 @@ Manually fixable by
386
386
  | [valid-describe-callback](docs/rules/valid-describe-callback.md) | Enforce valid `describe()` callback | ✅ | | | |
387
387
  | [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ✅ | | 🔧 | |
388
388
  | [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Require promises that have expectations in their chain to be valid | ✅ | | | |
389
+ | [valid-mock-module-path](docs/rules/valid-mock-module-path.md) | Disallow mocking of non-existing module paths | | | | |
389
390
  | [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ✅ | | 🔧 | |
390
391
 
391
392
  ### Requires Type Checking
@@ -0,0 +1,70 @@
1
+ # Disallow mocking of non-existing module paths (`valid-mock-module-path`)
2
+
3
+ <!-- end auto-generated rule header -->
4
+
5
+ This rule raises an error when using `jest.mock` and `jest.doMock` and the first
6
+ argument for mocked object (module/local file) do not exist.
7
+
8
+ ## Rule details
9
+
10
+ This rule checks existence of the supplied path for `jest.mock` or `jest.doMock`
11
+ in the first argument.
12
+
13
+ The following patterns are considered errors:
14
+
15
+ ```js
16
+ // Module(s) that cannot be found
17
+ jest.mock('@org/some-module-not-in-package-json');
18
+ jest.mock('some-module-not-in-package-json');
19
+
20
+ // Local module (directory) that cannot be found
21
+ jest.mock('../../this/module/does/not/exist');
22
+
23
+ // Local file that cannot be found
24
+ jest.mock('../../this/path/does/not/exist.js');
25
+ ```
26
+
27
+ The following patterns are **not** considered errors:
28
+
29
+ ```js
30
+ // Module(s) that can be found
31
+ jest.mock('@org/some-module-in-package-json');
32
+ jest.mock('some-module-in-package-json');
33
+
34
+ // Local module that cannot be found
35
+ jest.mock('../../this/module/really/does/exist');
36
+
37
+ // Local file that cannot be found
38
+ jest.mock('../../this/path/really/does/exist.js');
39
+ ```
40
+
41
+ ## Options
42
+
43
+ ```json
44
+ {
45
+ "jest/valid-mock-module-path": [
46
+ "error",
47
+ {
48
+ "moduleFileExtensions": [".js", ".ts", ".jsx", ".tsx", ".json"]
49
+ }
50
+ ]
51
+ }
52
+ ```
53
+
54
+ ### `moduleFileExtensions`
55
+
56
+ This array option controls which file extensions the plugin checks for
57
+ existence. The default extensions are:
58
+
59
+ - `".js"`
60
+ - `".ts"`
61
+ - `".jsx"`
62
+ - `".tsx"`
63
+ - `".json"`
64
+
65
+ For any custom extension, a preceding dot **must** be present before the file
66
+ extension for desired effect.
67
+
68
+ ## When Not To Use It
69
+
70
+ Don't use this rule on non-jest test files.
@@ -6,12 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _utils = require("@typescript-eslint/utils");
8
8
  var _utils2 = require("./utils");
9
- const findModuleName = node => {
10
- if (node.type === _utils.AST_NODE_TYPES.Literal && typeof node.value === 'string') {
11
- return node;
12
- }
13
- return null;
14
- };
15
9
  var _default = exports.default = (0, _utils2.createRule)({
16
10
  name: __filename,
17
11
  meta: {
@@ -46,7 +40,7 @@ var _default = exports.default = (0, _utils2.createRule)({
46
40
  if (hasTypeParameter || hasReturnType) {
47
41
  return;
48
42
  }
49
- const moduleName = findModuleName(nameNode);
43
+ const moduleName = (0, _utils2.findModuleName)(nameNode);
50
44
  context.report({
51
45
  messageId: 'addTypeParameterToModuleMock',
52
46
  data: {
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.getFirstMatcherArg = exports.findTopMostCallExpression = exports.createRule = exports.TestCaseName = exports.ModifierName = exports.HookName = exports.EqualityMatcher = exports.DescribeAlias = void 0;
6
+ exports.getFirstMatcherArg = exports.findTopMostCallExpression = exports.findModuleName = exports.createRule = exports.TestCaseName = exports.ModifierName = exports.HookName = exports.EqualityMatcher = exports.DescribeAlias = void 0;
7
7
  exports.getNodeName = getNodeName;
8
8
  exports.replaceAccessorFixer = exports.removeExtraArgumentsFixer = exports.isFunction = exports.isBooleanLiteral = exports.hasOnlyOneArgument = exports.getTestCallExpressionsFromDeclaredVariables = void 0;
9
9
  var _path = require("path");
@@ -158,4 +158,11 @@ const getFirstMatcherArg = expectFnCall => {
158
158
  }
159
159
  return (0, _followTypeAssertionChain.followTypeAssertionChain)(firstArg);
160
160
  };
161
- exports.getFirstMatcherArg = getFirstMatcherArg;
161
+ exports.getFirstMatcherArg = getFirstMatcherArg;
162
+ const findModuleName = node => {
163
+ if (node.type === _utils.AST_NODE_TYPES.Literal && typeof node.value === 'string') {
164
+ return node;
165
+ }
166
+ return null;
167
+ };
168
+ exports.findModuleName = findModuleName;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _fs = require("fs");
8
+ var _path = _interopRequireDefault(require("path"));
9
+ var _utils = require("@typescript-eslint/utils");
10
+ var _utils2 = require("./utils");
11
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
+ var _default = exports.default = (0, _utils2.createRule)({
13
+ name: __filename,
14
+ meta: {
15
+ type: 'problem',
16
+ docs: {
17
+ description: 'Disallow mocking of non-existing module paths'
18
+ },
19
+ messages: {
20
+ invalidMockModulePath: 'Module path {{ moduleName }} does not exist or is not exported'
21
+ },
22
+ schema: [{
23
+ type: 'object',
24
+ properties: {
25
+ moduleFileExtensions: {
26
+ type: 'array',
27
+ items: {
28
+ type: 'string'
29
+ },
30
+ additionalItems: false
31
+ }
32
+ },
33
+ additionalProperties: false
34
+ }]
35
+ },
36
+ defaultOptions: [{
37
+ moduleFileExtensions: ['.js', '.ts', '.tsx', '.jsx', '.json']
38
+ }],
39
+ create(context, [{
40
+ moduleFileExtensions = ['.js', '.ts', '.tsx', '.jsx', '.json']
41
+ }]) {
42
+ return {
43
+ CallExpression(node) {
44
+ if (node.callee.type !== _utils.AST_NODE_TYPES.MemberExpression) {
45
+ return;
46
+ }
47
+ if (!node.arguments.length || !(0, _utils2.isTypeOfJestFnCall)(node, context, ['jest']) || !((0, _utils2.isSupportedAccessor)(node.callee.property) && ['mock', 'doMock'].includes((0, _utils2.getAccessorValue)(node.callee.property)))) {
48
+ return;
49
+ }
50
+ const moduleName = (0, _utils2.findModuleName)(node.arguments[0]);
51
+ if (!moduleName) {
52
+ return;
53
+ }
54
+ try {
55
+ if (!moduleName.value.startsWith('.')) {
56
+ require.resolve(moduleName.value);
57
+ return;
58
+ }
59
+ const resolvedModulePath = _path.default.resolve(_path.default.dirname(context.filename), moduleName.value);
60
+ const hasPossiblyModulePaths = ['', ...moduleFileExtensions].some(ext => {
61
+ try {
62
+ (0, _fs.statSync)(`${resolvedModulePath}${ext}`);
63
+ return true;
64
+ } catch {
65
+ return false;
66
+ }
67
+ });
68
+ if (hasPossiblyModulePaths) {
69
+ return;
70
+ }
71
+ } catch (err) {
72
+ const castedErr = err;
73
+
74
+ // Reports unexpected issues when attempt to verify mocked module path.
75
+ // The list of possible errors is non-exhaustive.
76
+ if (castedErr.code !== 'MODULE_NOT_FOUND' && castedErr.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
77
+ throw new Error(`Error when trying to validate mock module path from \`jest.mock\`: ${err}`);
78
+ }
79
+ }
80
+ context.report({
81
+ messageId: 'invalidMockModulePath',
82
+ data: {
83
+ moduleName: moduleName.raw
84
+ },
85
+ node
86
+ });
87
+ }
88
+ };
89
+ }
90
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "29.1.0",
3
+ "version": "29.2.1",
4
4
  "description": "ESLint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",
@@ -134,7 +134,7 @@
134
134
  "optional": true
135
135
  }
136
136
  },
137
- "packageManager": "yarn@4.11.0",
137
+ "packageManager": "yarn@4.12.0",
138
138
  "engines": {
139
139
  "node": "^20.12.0 || ^22.0.0 || >=24.0.0"
140
140
  }