eslint-plugin-jest 25.2.2 → 25.3.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/CHANGELOG.md +28 -0
- package/README.md +1 -1
- package/docs/rules/prefer-lowercase-title.md +6 -6
- package/docs/rules/require-hook.md +37 -0
- package/lib/rules/prefer-to-be.js +11 -4
- package/lib/rules/require-hook.js +25 -6
- package/package.json +10 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [25.3.1](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.0...v25.3.1) (2021-12-27)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **prefer-to-be:** support template literals ([#1006](https://github.com/jest-community/eslint-plugin-jest/issues/1006)) ([aa428e6](https://github.com/jest-community/eslint-plugin-jest/commit/aa428e6598d5f7b259d3cec1bc505989a0fe9885))
|
|
7
|
+
|
|
8
|
+
# [25.3.0](https://github.com/jest-community/eslint-plugin-jest/compare/v25.2.4...v25.3.0) (2021-11-23)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* **require-hook:** add `allowedFunctionCalls` setting ([#983](https://github.com/jest-community/eslint-plugin-jest/issues/983)) ([9d9336a](https://github.com/jest-community/eslint-plugin-jest/commit/9d9336a7624c53c0bb3ee899b8cc336a0b3349cb))
|
|
14
|
+
|
|
15
|
+
## [25.2.4](https://github.com/jest-community/eslint-plugin-jest/compare/v25.2.3...v25.2.4) (2021-11-08)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* **prefer-to-be:** preserve `resolves` and `rejects` modifiers ([#980](https://github.com/jest-community/eslint-plugin-jest/issues/980)) ([a1296bd](https://github.com/jest-community/eslint-plugin-jest/commit/a1296bdee3a3a8ec5f64f95735ca01b91e8f4118))
|
|
21
|
+
|
|
22
|
+
## [25.2.3](https://github.com/jest-community/eslint-plugin-jest/compare/v25.2.2...v25.2.3) (2021-11-04)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* **no-deprecated-functions:** mark jest as an optional peer dependency ([#970](https://github.com/jest-community/eslint-plugin-jest/issues/970)) ([f468752](https://github.com/jest-community/eslint-plugin-jest/commit/f468752fc0aba89dd9bcce5fe676a04cb2fa6407))
|
|
28
|
+
|
|
1
29
|
## [25.2.2](https://github.com/jest-community/eslint-plugin-jest/compare/v25.2.1...v25.2.2) (2021-10-17)
|
|
2
30
|
|
|
3
31
|
|
package/README.md
CHANGED
|
@@ -128,7 +128,7 @@ config file:
|
|
|
128
128
|
```
|
|
129
129
|
|
|
130
130
|
See
|
|
131
|
-
[ESLint documentation](
|
|
131
|
+
[ESLint documentation](https://eslint.org/docs/user-guide/configuring/configuration-files#extending-configuration-files)
|
|
132
132
|
for more information about extending configuration files.
|
|
133
133
|
|
|
134
134
|
### All
|
|
@@ -26,7 +26,7 @@ it('adds 1 + 2 to equal 3', () => {
|
|
|
26
26
|
|
|
27
27
|
```json
|
|
28
28
|
{
|
|
29
|
-
"jest/prefer-lowercase-
|
|
29
|
+
"jest/prefer-lowercase-title": [
|
|
30
30
|
"error",
|
|
31
31
|
{
|
|
32
32
|
"ignore": ["describe", "test"]
|
|
@@ -50,7 +50,7 @@ By default, none of these options are enabled (the equivalent of
|
|
|
50
50
|
Example of **correct** code for the `{ "ignore": ["describe"] }` option:
|
|
51
51
|
|
|
52
52
|
```js
|
|
53
|
-
/* eslint jest/prefer-lowercase-
|
|
53
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["describe"] }] */
|
|
54
54
|
|
|
55
55
|
describe('Uppercase description');
|
|
56
56
|
```
|
|
@@ -58,7 +58,7 @@ describe('Uppercase description');
|
|
|
58
58
|
Example of **correct** code for the `{ "ignore": ["test"] }` option:
|
|
59
59
|
|
|
60
60
|
```js
|
|
61
|
-
/* eslint jest/prefer-lowercase-
|
|
61
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["test"] }] */
|
|
62
62
|
|
|
63
63
|
test('Uppercase description');
|
|
64
64
|
```
|
|
@@ -66,7 +66,7 @@ test('Uppercase description');
|
|
|
66
66
|
Example of **correct** code for the `{ "ignore": ["it"] }` option:
|
|
67
67
|
|
|
68
68
|
```js
|
|
69
|
-
/* eslint jest/prefer-lowercase-
|
|
69
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignore": ["it"] }] */
|
|
70
70
|
|
|
71
71
|
it('Uppercase description');
|
|
72
72
|
```
|
|
@@ -82,7 +82,7 @@ By default, nothing is allowed (the equivalent of `{ "allowedPrefixes": [] }`).
|
|
|
82
82
|
Example of **correct** code for the `{ "allowedPrefixes": ["GET"] }` option:
|
|
83
83
|
|
|
84
84
|
```js
|
|
85
|
-
/* eslint jest/prefer-lowercase-
|
|
85
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "allowedPrefixes": ["GET"] }] */
|
|
86
86
|
|
|
87
87
|
describe('GET /live');
|
|
88
88
|
```
|
|
@@ -95,7 +95,7 @@ title starting with an upper-case letter.
|
|
|
95
95
|
Example of **correct** code for the `{ "ignoreTopLevelDescribe": true }` option:
|
|
96
96
|
|
|
97
97
|
```js
|
|
98
|
-
/* eslint jest/prefer-lowercase-
|
|
98
|
+
/* eslint jest/prefer-lowercase-title: ["error", { "ignoreTopLevelDescribe": true }] */
|
|
99
99
|
describe('MyClass', () => {
|
|
100
100
|
describe('#myMethod', () => {
|
|
101
101
|
it('does things', () => {
|
|
@@ -148,3 +148,40 @@ afterEach(() => {
|
|
|
148
148
|
clearCityDatabase();
|
|
149
149
|
});
|
|
150
150
|
```
|
|
151
|
+
|
|
152
|
+
## Options
|
|
153
|
+
|
|
154
|
+
If there are methods that you want to call outside of hooks and tests, you can
|
|
155
|
+
mark them as allowed using the `allowedFunctionCalls` option.
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"jest/require-hook": [
|
|
160
|
+
"error",
|
|
161
|
+
{
|
|
162
|
+
"allowedFunctionCalls": ["enableAutoDestroy"]
|
|
163
|
+
}
|
|
164
|
+
]
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Examples of **correct** code when using
|
|
169
|
+
`{ "allowedFunctionCalls": ["enableAutoDestroy"] }` option:
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
/* eslint jest/require-hook: ["error", { "allowedFunctionCalls": ["enableAutoDestroy"] }] */
|
|
173
|
+
|
|
174
|
+
import { enableAutoDestroy, mount } from '@vue/test-utils';
|
|
175
|
+
import { initDatabase, tearDownDatabase } from './databaseUtils';
|
|
176
|
+
|
|
177
|
+
enableAutoDestroy(afterEach);
|
|
178
|
+
|
|
179
|
+
beforeEach(initDatabase);
|
|
180
|
+
afterEach(tearDownDatabase);
|
|
181
|
+
|
|
182
|
+
describe('Foo', () => {
|
|
183
|
+
test('always returns 42', () => {
|
|
184
|
+
expect(global.getAnswer()).toBe(42);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
```
|
|
@@ -20,9 +20,16 @@ const isNullEqualityMatcher = matcher => isNullLiteral(getFirstArgument(matcher)
|
|
|
20
20
|
|
|
21
21
|
const isFirstArgumentIdentifier = (matcher, name) => (0, _utils.isIdentifier)(getFirstArgument(matcher), name);
|
|
22
22
|
|
|
23
|
-
const
|
|
23
|
+
const shouldUseToBe = matcher => {
|
|
24
24
|
const firstArg = getFirstArgument(matcher);
|
|
25
|
-
|
|
25
|
+
|
|
26
|
+
if (firstArg.type === _experimentalUtils.AST_NODE_TYPES.Literal) {
|
|
27
|
+
// regex literals are classed as literals, but they're actually objects
|
|
28
|
+
// which means "toBe" will give different results than other matchers
|
|
29
|
+
return !('regex' in firstArg);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return firstArg.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral;
|
|
26
33
|
};
|
|
27
34
|
|
|
28
35
|
const getFirstArgument = matcher => {
|
|
@@ -30,7 +37,7 @@ const getFirstArgument = matcher => {
|
|
|
30
37
|
};
|
|
31
38
|
|
|
32
39
|
const reportPreferToBe = (context, whatToBe, matcher, modifier) => {
|
|
33
|
-
const modifierNode = (modifier === null || modifier === void 0 ? void 0 : modifier.negation) || (modifier === null || modifier === void 0 ? void 0 : modifier.node);
|
|
40
|
+
const modifierNode = (modifier === null || modifier === void 0 ? void 0 : modifier.negation) || (modifier === null || modifier === void 0 ? void 0 : modifier.name) === _utils.ModifierName.not && (modifier === null || modifier === void 0 ? void 0 : modifier.node);
|
|
34
41
|
context.report({
|
|
35
42
|
messageId: `useToBe${whatToBe}`,
|
|
36
43
|
|
|
@@ -116,7 +123,7 @@ var _default = (0, _utils.createRule)({
|
|
|
116
123
|
return;
|
|
117
124
|
}
|
|
118
125
|
|
|
119
|
-
if (
|
|
126
|
+
if (shouldUseToBe(matcher) && matcher.name !== _utils.EqualityMatcher.toBe) {
|
|
120
127
|
reportPreferToBe(context, '', matcher);
|
|
121
128
|
}
|
|
122
129
|
}
|
|
@@ -23,13 +23,13 @@ const isNullOrUndefined = node => {
|
|
|
23
23
|
return node.type === _experimentalUtils.AST_NODE_TYPES.Literal && node.value === null || (0, _utils.isIdentifier)(node, 'undefined');
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
const shouldBeInHook = node => {
|
|
26
|
+
const shouldBeInHook = (node, allowedFunctionCalls = []) => {
|
|
27
27
|
switch (node.type) {
|
|
28
28
|
case _experimentalUtils.AST_NODE_TYPES.ExpressionStatement:
|
|
29
|
-
return shouldBeInHook(node.expression);
|
|
29
|
+
return shouldBeInHook(node.expression, allowedFunctionCalls);
|
|
30
30
|
|
|
31
31
|
case _experimentalUtils.AST_NODE_TYPES.CallExpression:
|
|
32
|
-
return !isJestFnCall(node);
|
|
32
|
+
return !(isJestFnCall(node) || allowedFunctionCalls.includes((0, _utils.getNodeName)(node)));
|
|
33
33
|
|
|
34
34
|
case _experimentalUtils.AST_NODE_TYPES.VariableDeclaration:
|
|
35
35
|
{
|
|
@@ -59,14 +59,33 @@ var _default = (0, _utils.createRule)({
|
|
|
59
59
|
useHook: 'This should be done within a hook'
|
|
60
60
|
},
|
|
61
61
|
type: 'suggestion',
|
|
62
|
-
schema: [
|
|
62
|
+
schema: [{
|
|
63
|
+
type: 'object',
|
|
64
|
+
properties: {
|
|
65
|
+
allowedFunctionCalls: {
|
|
66
|
+
type: 'array',
|
|
67
|
+
items: {
|
|
68
|
+
type: 'string'
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
additionalProperties: false
|
|
73
|
+
}]
|
|
63
74
|
},
|
|
64
|
-
defaultOptions: [
|
|
75
|
+
defaultOptions: [{
|
|
76
|
+
allowedFunctionCalls: []
|
|
77
|
+
}],
|
|
65
78
|
|
|
66
79
|
create(context) {
|
|
80
|
+
var _context$options$;
|
|
81
|
+
|
|
82
|
+
const {
|
|
83
|
+
allowedFunctionCalls
|
|
84
|
+
} = (_context$options$ = context.options[0]) !== null && _context$options$ !== void 0 ? _context$options$ : {};
|
|
85
|
+
|
|
67
86
|
const checkBlockBody = body => {
|
|
68
87
|
for (const statement of body) {
|
|
69
|
-
if (shouldBeInHook(statement)) {
|
|
88
|
+
if (shouldBeInHook(statement, allowedFunctionCalls)) {
|
|
70
89
|
context.report({
|
|
71
90
|
node: statement,
|
|
72
91
|
messageId: 'useHook'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-jest",
|
|
3
|
-
"version": "25.
|
|
3
|
+
"version": "25.3.1",
|
|
4
4
|
"description": "Eslint rules for Jest",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -89,14 +89,14 @@
|
|
|
89
89
|
"@babel/core": "^7.4.4",
|
|
90
90
|
"@babel/preset-env": "^7.4.4",
|
|
91
91
|
"@babel/preset-typescript": "^7.3.3",
|
|
92
|
-
"@commitlint/cli": "^
|
|
93
|
-
"@commitlint/config-conventional": "^
|
|
92
|
+
"@commitlint/cli": "^15.0.0",
|
|
93
|
+
"@commitlint/config-conventional": "^15.0.0",
|
|
94
94
|
"@schemastore/package": "^0.0.6",
|
|
95
95
|
"@semantic-release/changelog": "^6.0.0",
|
|
96
96
|
"@semantic-release/git": "^10.0.0",
|
|
97
97
|
"@types/dedent": "^0.7.0",
|
|
98
98
|
"@types/jest": "^27.0.0",
|
|
99
|
-
"@types/node": "^
|
|
99
|
+
"@types/node": "^16.0.0",
|
|
100
100
|
"@types/prettier": "^2.0.0",
|
|
101
101
|
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
|
102
102
|
"@typescript-eslint/parser": "^5.0.0",
|
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
"is-ci": "^3.0.0",
|
|
116
116
|
"jest": "^27.0.0",
|
|
117
117
|
"jest-runner-eslint": "^1.0.0",
|
|
118
|
-
"lint-staged": "^
|
|
118
|
+
"lint-staged": "^12.0.0",
|
|
119
119
|
"pinst": "^2.0.0",
|
|
120
120
|
"prettier": "^2.0.5",
|
|
121
121
|
"rimraf": "^3.0.0",
|
|
@@ -131,6 +131,9 @@
|
|
|
131
131
|
"peerDependenciesMeta": {
|
|
132
132
|
"@typescript-eslint/eslint-plugin": {
|
|
133
133
|
"optional": true
|
|
134
|
+
},
|
|
135
|
+
"jest": {
|
|
136
|
+
"optional": true
|
|
134
137
|
}
|
|
135
138
|
},
|
|
136
139
|
"engines": {
|
|
@@ -155,5 +158,6 @@
|
|
|
155
158
|
},
|
|
156
159
|
"resolutions": {
|
|
157
160
|
"@typescript-eslint/experimental-utils": "^5.0.0"
|
|
158
|
-
}
|
|
161
|
+
},
|
|
162
|
+
"packageManager": "yarn@3.1.1"
|
|
159
163
|
}
|