eslint-plugin-jest 29.0.2-next.1 → 29.2.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 +1 -0
- package/docs/rules/prefer-lowercase-title.md +15 -0
- package/docs/rules/valid-mock-module-path.md +70 -0
- package/docs/rules/valid-title.md +4 -3
- package/lib/rules/no-untyped-mock-factory.js +1 -7
- package/lib/rules/prefer-lowercase-title.js +13 -2
- package/lib/rules/utils/misc.js +9 -2
- package/lib/rules/valid-mock-module-path.js +90 -0
- package/package.json +4 -3
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
|
|
@@ -109,3 +109,18 @@ describe('MyClass', () => {
|
|
|
109
109
|
});
|
|
110
110
|
});
|
|
111
111
|
```
|
|
112
|
+
|
|
113
|
+
### `ignoreTodos`
|
|
114
|
+
|
|
115
|
+
This option is used to control whether
|
|
116
|
+
[`todo`](https://jestjs.io/docs/api#testtodoname) Jest functions to be checked
|
|
117
|
+
by this rule. By the default, the option is set to false.
|
|
118
|
+
|
|
119
|
+
Example of **correct** code for the `{ "ignoreTodos": true }` option:
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignoreTodos": true }] */
|
|
123
|
+
test.todo('Uppercase description');
|
|
124
|
+
|
|
125
|
+
it.todo('Uppercase description');
|
|
126
|
+
```
|
|
@@ -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.
|
|
@@ -175,6 +175,7 @@ describe('foo', () => {
|
|
|
175
175
|
interface Options {
|
|
176
176
|
ignoreSpaces?: boolean;
|
|
177
177
|
ignoreTypeOfDescribeName?: boolean;
|
|
178
|
+
ignoreTypeOfTestName?: boolean;
|
|
178
179
|
disallowedWords?: string[];
|
|
179
180
|
mustNotMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
|
|
180
181
|
mustMatch?: Partial<Record<'describe' | 'test' | 'it', string>> | string;
|
|
@@ -187,12 +188,12 @@ Default: `false`
|
|
|
187
188
|
|
|
188
189
|
When enabled, the leading and trailing spaces won't be checked.
|
|
189
190
|
|
|
190
|
-
#### `ignoreTypeOfDescribeName`
|
|
191
|
+
#### `ignoreTypeOfDescribeName` & `ignoreTypeOfTestName`
|
|
191
192
|
|
|
192
193
|
Default: `false`
|
|
193
194
|
|
|
194
|
-
When enabled, the type of the first argument to `describe` blocks won't
|
|
195
|
-
checked.
|
|
195
|
+
When enabled, the type of the first argument to `describe`/`test` blocks won't
|
|
196
|
+
be checked.
|
|
196
197
|
|
|
197
198
|
#### `disallowedWords`
|
|
198
199
|
|
|
@@ -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: {
|
|
@@ -53,6 +53,10 @@ var _default = exports.default = (0, _utils.createRule)({
|
|
|
53
53
|
ignoreTopLevelDescribe: {
|
|
54
54
|
type: 'boolean',
|
|
55
55
|
default: false
|
|
56
|
+
},
|
|
57
|
+
ignoreTodos: {
|
|
58
|
+
type: 'boolean',
|
|
59
|
+
default: false
|
|
56
60
|
}
|
|
57
61
|
},
|
|
58
62
|
additionalProperties: false
|
|
@@ -61,12 +65,14 @@ var _default = exports.default = (0, _utils.createRule)({
|
|
|
61
65
|
defaultOptions: [{
|
|
62
66
|
ignore: [],
|
|
63
67
|
allowedPrefixes: [],
|
|
64
|
-
ignoreTopLevelDescribe: false
|
|
68
|
+
ignoreTopLevelDescribe: false,
|
|
69
|
+
ignoreTodos: false
|
|
65
70
|
}],
|
|
66
71
|
create(context, [{
|
|
67
72
|
ignore = [],
|
|
68
73
|
allowedPrefixes = [],
|
|
69
|
-
ignoreTopLevelDescribe
|
|
74
|
+
ignoreTopLevelDescribe,
|
|
75
|
+
ignoreTodos
|
|
70
76
|
}]) {
|
|
71
77
|
const ignores = populateIgnores(ignore);
|
|
72
78
|
let numberOfDescribeBlocks = 0;
|
|
@@ -84,6 +90,11 @@ var _default = exports.default = (0, _utils.createRule)({
|
|
|
84
90
|
} else if (jestFnCall.type !== 'test') {
|
|
85
91
|
return;
|
|
86
92
|
}
|
|
93
|
+
|
|
94
|
+
// Ignore *.todo test and/or test suites
|
|
95
|
+
if (ignoreTodos && jestFnCall.members.some(s => (0, _utils.getAccessorValue)(s) === 'todo')) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
87
98
|
const [firstArg] = node.arguments;
|
|
88
99
|
const description = (0, _utils.getStringValue)(firstArg);
|
|
89
100
|
if (allowedPrefixes.some(name => description.startsWith(name))) {
|
package/lib/rules/utils/misc.js
CHANGED
|
@@ -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'
|
|
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') {
|
|
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.
|
|
3
|
+
"version": "29.2.0",
|
|
4
4
|
"description": "ESLint rules for Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -94,13 +94,14 @@
|
|
|
94
94
|
"@types/semver": "^7.5.8",
|
|
95
95
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
96
96
|
"@typescript-eslint/parser": "^8.0.0",
|
|
97
|
+
"@typescript-eslint/utils": "^8.0.0",
|
|
97
98
|
"babel-jest": "^30.0.0",
|
|
98
99
|
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
|
|
99
100
|
"dedent": "^1.5.0",
|
|
100
101
|
"eslint": "^8.57.0",
|
|
101
102
|
"eslint-config-prettier": "^10.0.0",
|
|
102
103
|
"eslint-doc-generator": "^2.0.0",
|
|
103
|
-
"eslint-plugin-eslint-plugin": "
|
|
104
|
+
"eslint-plugin-eslint-plugin": "^6.0.0",
|
|
104
105
|
"eslint-plugin-import": "^2.25.1",
|
|
105
106
|
"eslint-plugin-n": "^17.0.0",
|
|
106
107
|
"eslint-plugin-prettier": "^5.0.0",
|
|
@@ -133,7 +134,7 @@
|
|
|
133
134
|
"optional": true
|
|
134
135
|
}
|
|
135
136
|
},
|
|
136
|
-
"packageManager": "yarn@4.
|
|
137
|
+
"packageManager": "yarn@4.11.0",
|
|
137
138
|
"engines": {
|
|
138
139
|
"node": "^20.12.0 || ^22.0.0 || >=24.0.0"
|
|
139
140
|
}
|