eslint-plugin-jest 25.5.0 → 26.1.0-next.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 +3 -0
- package/docs/rules/no-conditional-in-test.md +79 -0
- package/docs/rules/prefer-comparison-matcher.md +55 -0
- package/docs/rules/prefer-equality-matcher.md +29 -0
- package/docs/rules/valid-expect.md +13 -0
- package/lib/rules/consistent-test-it.js +20 -20
- package/lib/rules/expect-expect.js +9 -9
- package/lib/rules/max-nested-describe.js +5 -5
- package/lib/rules/no-conditional-expect.js +9 -9
- package/lib/rules/no-conditional-in-test.js +60 -0
- package/lib/rules/no-deprecated-functions.js +6 -6
- package/lib/rules/no-done-callback.js +10 -10
- package/lib/rules/no-export.js +6 -6
- package/lib/rules/no-focused-tests.js +11 -11
- package/lib/rules/no-if.js +11 -11
- package/lib/rules/no-interpolation-in-snapshots.js +6 -6
- package/lib/rules/no-jasmine-globals.js +10 -10
- package/lib/rules/no-large-snapshots.js +8 -8
- package/lib/rules/no-standalone-expect.js +14 -14
- package/lib/rules/no-test-prefixes.js +6 -6
- package/lib/rules/no-test-return-statement.js +8 -8
- package/lib/rules/prefer-comparison-matcher.js +139 -0
- package/lib/rules/prefer-equality-matcher.js +98 -0
- package/lib/rules/prefer-expect-assertions.js +12 -12
- package/lib/rules/prefer-expect-resolves.js +4 -4
- package/lib/rules/prefer-spy-on.js +9 -9
- package/lib/rules/prefer-to-be.js +15 -15
- package/lib/rules/prefer-to-contain.js +11 -11
- package/lib/rules/prefer-to-have-length.js +6 -6
- package/lib/rules/prefer-todo.js +9 -9
- package/lib/rules/require-hook.js +12 -12
- package/lib/rules/utils.js +27 -27
- package/lib/rules/valid-describe-callback.js +9 -9
- package/lib/rules/valid-expect-in-promise.js +44 -44
- package/lib/rules/valid-expect.js +29 -18
- package/lib/rules/valid-title.js +14 -14
- package/package.json +9 -7
- package/CHANGELOG.md +0 -831
package/README.md
CHANGED
|
@@ -158,6 +158,7 @@ installations requiring long-term consistency.
|
|
|
158
158
|
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
|
|
159
159
|
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
|
|
160
160
|
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
|
|
161
|
+
| [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests | | |
|
|
161
162
|
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
|
|
162
163
|
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
|
|
163
164
|
| [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
|
|
@@ -177,6 +178,8 @@ installations requiring long-term consistency.
|
|
|
177
178
|
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
|
|
178
179
|
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
|
|
179
180
|
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
|
|
181
|
+
| [prefer-comparison-matcher](docs/rules/prefer-comparison-matcher.md) | Suggest using the built-in comparison matchers | | ![fixable][] |
|
|
182
|
+
| [prefer-equality-matcher](docs/rules/prefer-equality-matcher.md) | Suggest using the built-in equality matchers | | ![suggest][] |
|
|
180
183
|
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
|
|
181
184
|
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
|
|
182
185
|
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Disallow conditional logic in tests (`no-conditional-in-test`)
|
|
2
|
+
|
|
3
|
+
Conditional logic in tests is usually an indication that a test is attempting to
|
|
4
|
+
cover too much, and not testing the logic it intends to. Each branch of code
|
|
5
|
+
executing within a conditional statement will usually be better served by a test
|
|
6
|
+
devoted to it.
|
|
7
|
+
|
|
8
|
+
## Rule Details
|
|
9
|
+
|
|
10
|
+
This rule reports on any use of a conditional statement such as `if`, `switch`,
|
|
11
|
+
and ternary expressions.
|
|
12
|
+
|
|
13
|
+
Examples of **incorrect** code for this rule:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
it('foo', () => {
|
|
17
|
+
if (true) {
|
|
18
|
+
doTheThing();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('bar', () => {
|
|
23
|
+
switch (mode) {
|
|
24
|
+
case 'none':
|
|
25
|
+
generateNone();
|
|
26
|
+
case 'single':
|
|
27
|
+
generateOne();
|
|
28
|
+
case 'multiple':
|
|
29
|
+
generateMany();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
expect(fixtures.length).toBeGreaterThan(-1);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('baz', async () => {
|
|
36
|
+
const promiseValue = () => {
|
|
37
|
+
return something instanceof Promise
|
|
38
|
+
? something
|
|
39
|
+
: Promise.resolve(something);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
await expect(promiseValue()).resolves.toBe(1);
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Examples of **correct** code for this rule:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
describe('my tests', () => {
|
|
50
|
+
if (true) {
|
|
51
|
+
it('foo', () => {
|
|
52
|
+
doTheThing();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
switch (mode) {
|
|
59
|
+
case 'none':
|
|
60
|
+
generateNone();
|
|
61
|
+
case 'single':
|
|
62
|
+
generateOne();
|
|
63
|
+
case 'multiple':
|
|
64
|
+
generateMany();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('bar', () => {
|
|
69
|
+
expect(fixtures.length).toBeGreaterThan(-1);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const promiseValue = something => {
|
|
73
|
+
return something instanceof Promise ? something : Promise.resolve(something);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
it('baz', async () => {
|
|
77
|
+
await expect(promiseValue()).resolves.toBe(1);
|
|
78
|
+
});
|
|
79
|
+
```
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Suggest using the built-in comparison matchers (`prefer-comparison-matcher`)
|
|
2
|
+
|
|
3
|
+
Jest has a number of built-in matchers for comparing numbers which allow for
|
|
4
|
+
more readable tests and error messages if an expectation fails.
|
|
5
|
+
|
|
6
|
+
## Rule details
|
|
7
|
+
|
|
8
|
+
This rule checks for comparisons in tests that could be replaced with one of the
|
|
9
|
+
following built-in comparison matchers:
|
|
10
|
+
|
|
11
|
+
- `toBeGreaterThan`
|
|
12
|
+
- `toBeGreaterThanOrEqual`
|
|
13
|
+
- `toBeLessThan`
|
|
14
|
+
- `toBeLessThanOrEqual`
|
|
15
|
+
|
|
16
|
+
Examples of **incorrect** code for this rule:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
expect(x > 5).toBe(true);
|
|
20
|
+
expect(x < 7).not.toEqual(true);
|
|
21
|
+
expect(x <= y).toStrictEqual(true);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Examples of **correct** code for this rule:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
expect(x).toBeGreaterThan(5);
|
|
28
|
+
expect(x).not.toBeLessThanOrEqual(7);
|
|
29
|
+
expect(x).toBeLessThanOrEqual(y);
|
|
30
|
+
|
|
31
|
+
// special case - see below
|
|
32
|
+
expect(x < 'Carl').toBe(true);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Note that these matchers only work with numbers and bigints, and that the rule
|
|
36
|
+
assumes that any variables on either side of the comparison operator are of one
|
|
37
|
+
of those types - this means if you're using the comparison operator with
|
|
38
|
+
strings, the fix applied by this rule will result in an error.
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
expect(myName).toBeGreaterThanOrEqual(theirName); // Matcher error: received value must be a number or bigint
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The reason for this is that comparing strings with these operators is expected
|
|
45
|
+
to be very rare and would mean not being able to have an automatic fixer for
|
|
46
|
+
this rule.
|
|
47
|
+
|
|
48
|
+
If for some reason you are using these operators to compare strings, you can
|
|
49
|
+
disable this rule using an inline
|
|
50
|
+
[configuration comment](https://eslint.org/docs/user-guide/configuring/rules#disabling-rules):
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
// eslint-disable-next-line jest/prefer-comparison-matcher
|
|
54
|
+
expect(myName > theirName).toBe(true);
|
|
55
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Suggest using the built-in equality matchers (`prefer-equality-matcher`)
|
|
2
|
+
|
|
3
|
+
Jest has built-in matchers for expecting equality which allow for more readable
|
|
4
|
+
tests and error messages if an expectation fails.
|
|
5
|
+
|
|
6
|
+
## Rule details
|
|
7
|
+
|
|
8
|
+
This rule checks for _strict_ equality checks (`===` & `!==`) in tests that
|
|
9
|
+
could be replaced with one of the following built-in equality matchers:
|
|
10
|
+
|
|
11
|
+
- `toBe`
|
|
12
|
+
- `toEqual`
|
|
13
|
+
- `toStrictEqual`
|
|
14
|
+
|
|
15
|
+
Examples of **incorrect** code for this rule:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
expect(x === 5).toBe(true);
|
|
19
|
+
expect(name === 'Carl').not.toEqual(true);
|
|
20
|
+
expect(myObj !== thatObj).toStrictEqual(true);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Examples of **correct** code for this rule:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
expect(x).toBe(5);
|
|
27
|
+
expect(name).not.toEqual('Carl');
|
|
28
|
+
expect(myObj).toStrictEqual(thatObj);
|
|
29
|
+
```
|
|
@@ -38,6 +38,11 @@ This rule is enabled by default.
|
|
|
38
38
|
type: 'boolean',
|
|
39
39
|
default: false,
|
|
40
40
|
},
|
|
41
|
+
asyncMatchers: {
|
|
42
|
+
type: 'array',
|
|
43
|
+
items: { type: 'string' },
|
|
44
|
+
default: ['toResolve', 'toReject'],
|
|
45
|
+
},
|
|
41
46
|
minArgs: {
|
|
42
47
|
type: 'number',
|
|
43
48
|
minimum: 1,
|
|
@@ -78,6 +83,14 @@ test('test1', async () => {
|
|
|
78
83
|
test('test2', () => expect(Promise.resolve(2)).resolves.toBe(2));
|
|
79
84
|
```
|
|
80
85
|
|
|
86
|
+
### `asyncMatchers`
|
|
87
|
+
|
|
88
|
+
Allows specifying which matchers return promises, and so should be considered
|
|
89
|
+
async when checking if an `expect` should be returned or awaited.
|
|
90
|
+
|
|
91
|
+
By default, this has a list of all the async matchers provided by
|
|
92
|
+
`jest-extended` (namely, `toResolve` and `toReject`).
|
|
93
|
+
|
|
81
94
|
### `minArgs` & `maxArgs`
|
|
82
95
|
|
|
83
96
|
Enforces the minimum and maximum number of arguments that `expect` can take, and
|
|
@@ -5,13 +5,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _utils = require("@typescript-eslint/utils");
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _utils2 = require("./utils");
|
|
11
11
|
|
|
12
|
-
const buildFixer = (callee, nodeName, preferredTestKeyword) => fixer => [fixer.replaceText(callee.type ===
|
|
12
|
+
const buildFixer = (callee, nodeName, preferredTestKeyword) => fixer => [fixer.replaceText(callee.type === _utils.AST_NODE_TYPES.MemberExpression ? callee.object : callee, getPreferredNodeName(nodeName, preferredTestKeyword))];
|
|
13
13
|
|
|
14
|
-
var _default = (0,
|
|
14
|
+
var _default = (0, _utils2.createRule)({
|
|
15
15
|
name: __filename,
|
|
16
16
|
meta: {
|
|
17
17
|
docs: {
|
|
@@ -28,10 +28,10 @@ var _default = (0, _utils.createRule)({
|
|
|
28
28
|
type: 'object',
|
|
29
29
|
properties: {
|
|
30
30
|
fn: {
|
|
31
|
-
enum: [
|
|
31
|
+
enum: [_utils2.TestCaseName.it, _utils2.TestCaseName.test]
|
|
32
32
|
},
|
|
33
33
|
withinDescribe: {
|
|
34
|
-
enum: [
|
|
34
|
+
enum: [_utils2.TestCaseName.it, _utils2.TestCaseName.test]
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
additionalProperties: false
|
|
@@ -39,30 +39,30 @@ var _default = (0, _utils.createRule)({
|
|
|
39
39
|
type: 'suggestion'
|
|
40
40
|
},
|
|
41
41
|
defaultOptions: [{
|
|
42
|
-
fn:
|
|
43
|
-
withinDescribe:
|
|
42
|
+
fn: _utils2.TestCaseName.test,
|
|
43
|
+
withinDescribe: _utils2.TestCaseName.it
|
|
44
44
|
}],
|
|
45
45
|
|
|
46
46
|
create(context) {
|
|
47
47
|
const configObj = context.options[0] || {};
|
|
48
|
-
const testKeyword = configObj.fn ||
|
|
49
|
-
const testKeywordWithinDescribe = configObj.withinDescribe || configObj.fn ||
|
|
48
|
+
const testKeyword = configObj.fn || _utils2.TestCaseName.test;
|
|
49
|
+
const testKeywordWithinDescribe = configObj.withinDescribe || configObj.fn || _utils2.TestCaseName.it;
|
|
50
50
|
let describeNestingLevel = 0;
|
|
51
51
|
return {
|
|
52
52
|
CallExpression(node) {
|
|
53
|
-
const nodeName = (0,
|
|
53
|
+
const nodeName = (0, _utils2.getNodeName)(node.callee);
|
|
54
54
|
|
|
55
55
|
if (!nodeName) {
|
|
56
56
|
return;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
if ((0,
|
|
59
|
+
if ((0, _utils2.isDescribeCall)(node)) {
|
|
60
60
|
describeNestingLevel++;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
const funcNode = node.callee.type ===
|
|
63
|
+
const funcNode = node.callee.type === _utils.AST_NODE_TYPES.TaggedTemplateExpression ? node.callee.tag : node.callee.type === _utils.AST_NODE_TYPES.CallExpression ? node.callee.callee : node.callee;
|
|
64
64
|
|
|
65
|
-
if ((0,
|
|
65
|
+
if ((0, _utils2.isTestCaseCall)(node) && describeNestingLevel === 0 && !nodeName.includes(testKeyword)) {
|
|
66
66
|
const oppositeTestKeyword = getOppositeTestKeyword(testKeyword);
|
|
67
67
|
context.report({
|
|
68
68
|
messageId: 'consistentMethod',
|
|
@@ -75,7 +75,7 @@ var _default = (0, _utils.createRule)({
|
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
if ((0,
|
|
78
|
+
if ((0, _utils2.isTestCaseCall)(node) && describeNestingLevel > 0 && !nodeName.includes(testKeywordWithinDescribe)) {
|
|
79
79
|
const oppositeTestKeyword = getOppositeTestKeyword(testKeywordWithinDescribe);
|
|
80
80
|
context.report({
|
|
81
81
|
messageId: 'consistentMethodWithinDescribe',
|
|
@@ -90,7 +90,7 @@ var _default = (0, _utils.createRule)({
|
|
|
90
90
|
},
|
|
91
91
|
|
|
92
92
|
'CallExpression:exit'(node) {
|
|
93
|
-
if ((0,
|
|
93
|
+
if ((0, _utils2.isDescribeCall)(node)) {
|
|
94
94
|
describeNestingLevel--;
|
|
95
95
|
}
|
|
96
96
|
}
|
|
@@ -103,7 +103,7 @@ var _default = (0, _utils.createRule)({
|
|
|
103
103
|
exports.default = _default;
|
|
104
104
|
|
|
105
105
|
function getPreferredNodeName(nodeName, preferredTestKeyword) {
|
|
106
|
-
if (nodeName ===
|
|
106
|
+
if (nodeName === _utils2.TestCaseName.fit) {
|
|
107
107
|
return 'test.only';
|
|
108
108
|
}
|
|
109
109
|
|
|
@@ -111,9 +111,9 @@ function getPreferredNodeName(nodeName, preferredTestKeyword) {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
function getOppositeTestKeyword(test) {
|
|
114
|
-
if (test ===
|
|
115
|
-
return
|
|
114
|
+
if (test === _utils2.TestCaseName.test) {
|
|
115
|
+
return _utils2.TestCaseName.it;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
return
|
|
118
|
+
return _utils2.TestCaseName.test;
|
|
119
119
|
}
|
|
@@ -5,9 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _utils = require("@typescript-eslint/utils");
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _utils2 = require("./utils");
|
|
11
11
|
|
|
12
12
|
/*
|
|
13
13
|
* This implementation is adapted from eslint-plugin-jasmine.
|
|
@@ -28,7 +28,7 @@ function matchesAssertFunctionName(nodeName, patterns) {
|
|
|
28
28
|
}).join('\\.')}(\\.|$)`, 'ui').test(nodeName));
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
var _default = (0,
|
|
31
|
+
var _default = (0, _utils2.createRule)({
|
|
32
32
|
name: __filename,
|
|
33
33
|
meta: {
|
|
34
34
|
docs: {
|
|
@@ -72,11 +72,11 @@ var _default = (0, _utils.createRule)({
|
|
|
72
72
|
|
|
73
73
|
function checkCallExpressionUsed(nodes) {
|
|
74
74
|
for (const node of nodes) {
|
|
75
|
-
const index = node.type ===
|
|
75
|
+
const index = node.type === _utils.AST_NODE_TYPES.CallExpression ? unchecked.indexOf(node) : -1;
|
|
76
76
|
|
|
77
|
-
if (node.type ===
|
|
77
|
+
if (node.type === _utils.AST_NODE_TYPES.FunctionDeclaration) {
|
|
78
78
|
const declaredVariables = context.getDeclaredVariables(node);
|
|
79
|
-
const testCallExpressions = (0,
|
|
79
|
+
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
|
|
80
80
|
checkCallExpressionUsed(testCallExpressions);
|
|
81
81
|
}
|
|
82
82
|
|
|
@@ -91,10 +91,10 @@ var _default = (0, _utils.createRule)({
|
|
|
91
91
|
CallExpression(node) {
|
|
92
92
|
var _getNodeName;
|
|
93
93
|
|
|
94
|
-
const name = (_getNodeName = (0,
|
|
94
|
+
const name = (_getNodeName = (0, _utils2.getNodeName)(node.callee)) !== null && _getNodeName !== void 0 ? _getNodeName : '';
|
|
95
95
|
|
|
96
|
-
if ((0,
|
|
97
|
-
if (node.callee.type ===
|
|
96
|
+
if ((0, _utils2.isTestCaseCall)(node) || additionalTestBlockFunctions.includes(name)) {
|
|
97
|
+
if (node.callee.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.callee.property, 'todo')) {
|
|
98
98
|
return;
|
|
99
99
|
}
|
|
100
100
|
|
|
@@ -5,11 +5,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _utils = require("@typescript-eslint/utils");
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _utils2 = require("./utils");
|
|
11
11
|
|
|
12
|
-
var _default = (0,
|
|
12
|
+
var _default = (0, _utils2.createRule)({
|
|
13
13
|
name: __filename,
|
|
14
14
|
meta: {
|
|
15
15
|
docs: {
|
|
@@ -46,7 +46,7 @@ var _default = (0, _utils.createRule)({
|
|
|
46
46
|
parent
|
|
47
47
|
} = node;
|
|
48
48
|
|
|
49
|
-
if ((parent === null || parent === void 0 ? void 0 : parent.type) !==
|
|
49
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) !== _utils.AST_NODE_TYPES.CallExpression || !(0, _utils2.isDescribeCall)(parent)) {
|
|
50
50
|
return;
|
|
51
51
|
}
|
|
52
52
|
|
|
@@ -69,7 +69,7 @@ var _default = (0, _utils.createRule)({
|
|
|
69
69
|
parent
|
|
70
70
|
} = node;
|
|
71
71
|
|
|
72
|
-
if ((parent === null || parent === void 0 ? void 0 : parent.type) ===
|
|
72
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === _utils.AST_NODE_TYPES.CallExpression && (0, _utils2.isDescribeCall)(parent)) {
|
|
73
73
|
describeCallbackStack.pop();
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -5,13 +5,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _utils = require("@typescript-eslint/utils");
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _utils2 = require("./utils");
|
|
11
11
|
|
|
12
|
-
const isCatchCall = node => node.callee.type ===
|
|
12
|
+
const isCatchCall = node => node.callee.type === _utils.AST_NODE_TYPES.MemberExpression && (0, _utils2.isSupportedAccessor)(node.callee.property, 'catch');
|
|
13
13
|
|
|
14
|
-
var _default = (0,
|
|
14
|
+
var _default = (0, _utils2.createRule)({
|
|
15
15
|
name: __filename,
|
|
16
16
|
meta: {
|
|
17
17
|
docs: {
|
|
@@ -39,7 +39,7 @@ var _default = (0, _utils.createRule)({
|
|
|
39
39
|
return {
|
|
40
40
|
FunctionDeclaration(node) {
|
|
41
41
|
const declaredVariables = context.getDeclaredVariables(node);
|
|
42
|
-
const testCallExpressions = (0,
|
|
42
|
+
const testCallExpressions = (0, _utils2.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
|
|
43
43
|
|
|
44
44
|
if (testCallExpressions.length > 0) {
|
|
45
45
|
inTestCase = true;
|
|
@@ -47,7 +47,7 @@ var _default = (0, _utils.createRule)({
|
|
|
47
47
|
},
|
|
48
48
|
|
|
49
49
|
CallExpression(node) {
|
|
50
|
-
if ((0,
|
|
50
|
+
if ((0, _utils2.isTestCaseCall)(node)) {
|
|
51
51
|
inTestCase = true;
|
|
52
52
|
}
|
|
53
53
|
|
|
@@ -55,14 +55,14 @@ var _default = (0, _utils.createRule)({
|
|
|
55
55
|
inPromiseCatch = true;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
if (inTestCase && (0,
|
|
58
|
+
if (inTestCase && (0, _utils2.isExpectCall)(node) && conditionalDepth > 0) {
|
|
59
59
|
context.report({
|
|
60
60
|
messageId: 'conditionalExpect',
|
|
61
61
|
node
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
if (inPromiseCatch && (0,
|
|
65
|
+
if (inPromiseCatch && (0, _utils2.isExpectCall)(node)) {
|
|
66
66
|
context.report({
|
|
67
67
|
messageId: 'conditionalExpect',
|
|
68
68
|
node
|
|
@@ -71,7 +71,7 @@ var _default = (0, _utils.createRule)({
|
|
|
71
71
|
},
|
|
72
72
|
|
|
73
73
|
'CallExpression:exit'(node) {
|
|
74
|
-
if ((0,
|
|
74
|
+
if ((0, _utils2.isTestCaseCall)(node)) {
|
|
75
75
|
inTestCase = false;
|
|
76
76
|
}
|
|
77
77
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
|
|
10
|
+
var _default = (0, _utils.createRule)({
|
|
11
|
+
name: __filename,
|
|
12
|
+
meta: {
|
|
13
|
+
docs: {
|
|
14
|
+
description: 'Disallow conditional logic in tests',
|
|
15
|
+
category: 'Best Practices',
|
|
16
|
+
recommended: false
|
|
17
|
+
},
|
|
18
|
+
messages: {
|
|
19
|
+
conditionalInTest: 'Avoid having conditionals in tests'
|
|
20
|
+
},
|
|
21
|
+
type: 'problem',
|
|
22
|
+
schema: []
|
|
23
|
+
},
|
|
24
|
+
defaultOptions: [],
|
|
25
|
+
|
|
26
|
+
create(context) {
|
|
27
|
+
let inTestCase = false;
|
|
28
|
+
|
|
29
|
+
const maybeReportConditional = node => {
|
|
30
|
+
if (inTestCase) {
|
|
31
|
+
context.report({
|
|
32
|
+
messageId: 'conditionalInTest',
|
|
33
|
+
node
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
CallExpression(node) {
|
|
40
|
+
if ((0, _utils.isTestCaseCall)(node)) {
|
|
41
|
+
inTestCase = true;
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
'CallExpression:exit'(node) {
|
|
46
|
+
if ((0, _utils.isTestCaseCall)(node)) {
|
|
47
|
+
inTestCase = false;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
IfStatement: maybeReportConditional,
|
|
52
|
+
SwitchStatement: maybeReportConditional,
|
|
53
|
+
ConditionalExpression: maybeReportConditional,
|
|
54
|
+
LogicalExpression: maybeReportConditional
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
exports.default = _default;
|
|
@@ -5,11 +5,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _utils = require("@typescript-eslint/utils");
|
|
9
9
|
|
|
10
10
|
var _detectJestVersion = require("./detectJestVersion");
|
|
11
11
|
|
|
12
|
-
var
|
|
12
|
+
var _utils2 = require("./utils");
|
|
13
13
|
|
|
14
14
|
const parseJestVersion = rawVersion => {
|
|
15
15
|
if (typeof rawVersion === 'number') {
|
|
@@ -20,7 +20,7 @@ const parseJestVersion = rawVersion => {
|
|
|
20
20
|
return parseInt(majorVersion, 10);
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
var _default = (0,
|
|
23
|
+
var _default = (0, _utils2.createRule)({
|
|
24
24
|
name: __filename,
|
|
25
25
|
meta: {
|
|
26
26
|
docs: {
|
|
@@ -60,11 +60,11 @@ var _default = (0, _utils.createRule)({
|
|
|
60
60
|
};
|
|
61
61
|
return {
|
|
62
62
|
CallExpression(node) {
|
|
63
|
-
if (node.callee.type !==
|
|
63
|
+
if (node.callee.type !== _utils.AST_NODE_TYPES.MemberExpression) {
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
const deprecation = (0,
|
|
67
|
+
const deprecation = (0, _utils2.getNodeName)(node);
|
|
68
68
|
|
|
69
69
|
if (!deprecation || !(deprecation in deprecations)) {
|
|
70
70
|
return;
|
|
@@ -85,7 +85,7 @@ var _default = (0, _utils.createRule)({
|
|
|
85
85
|
fix(fixer) {
|
|
86
86
|
let [name, func] = replacement.split('.');
|
|
87
87
|
|
|
88
|
-
if (callee.property.type ===
|
|
88
|
+
if (callee.property.type === _utils.AST_NODE_TYPES.Literal) {
|
|
89
89
|
func = `'${func}'`;
|
|
90
90
|
}
|
|
91
91
|
|
|
@@ -5,27 +5,27 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _utils = require("@typescript-eslint/utils");
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _utils2 = require("./utils");
|
|
11
11
|
|
|
12
12
|
const findCallbackArg = (node, isJestEach) => {
|
|
13
13
|
if (isJestEach) {
|
|
14
14
|
return node.arguments[1];
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
if ((0,
|
|
17
|
+
if ((0, _utils2.isHook)(node) && node.arguments.length >= 1) {
|
|
18
18
|
return node.arguments[0];
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
if ((0,
|
|
21
|
+
if ((0, _utils2.isTestCaseCall)(node) && node.arguments.length >= 2) {
|
|
22
22
|
return node.arguments[1];
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
return null;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
var _default = (0,
|
|
28
|
+
var _default = (0, _utils2.createRule)({
|
|
29
29
|
name: __filename,
|
|
30
30
|
meta: {
|
|
31
31
|
docs: {
|
|
@@ -51,9 +51,9 @@ var _default = (0, _utils.createRule)({
|
|
|
51
51
|
var _getNodeName$endsWith, _getNodeName;
|
|
52
52
|
|
|
53
53
|
// done is the second argument for it.each, not the first
|
|
54
|
-
const isJestEach = (_getNodeName$endsWith = (_getNodeName = (0,
|
|
54
|
+
const isJestEach = (_getNodeName$endsWith = (_getNodeName = (0, _utils2.getNodeName)(node.callee)) === null || _getNodeName === void 0 ? void 0 : _getNodeName.endsWith('.each')) !== null && _getNodeName$endsWith !== void 0 ? _getNodeName$endsWith : false;
|
|
55
55
|
|
|
56
|
-
if (isJestEach && node.callee.type !==
|
|
56
|
+
if (isJestEach && node.callee.type !== _utils.AST_NODE_TYPES.TaggedTemplateExpression) {
|
|
57
57
|
// isJestEach but not a TaggedTemplateExpression, so this must be
|
|
58
58
|
// the `jest.each([])()` syntax which this rule doesn't support due
|
|
59
59
|
// to its complexity (see jest-community/eslint-plugin-jest#710)
|
|
@@ -63,13 +63,13 @@ var _default = (0, _utils.createRule)({
|
|
|
63
63
|
const callback = findCallbackArg(node, isJestEach);
|
|
64
64
|
const callbackArgIndex = Number(isJestEach);
|
|
65
65
|
|
|
66
|
-
if (!callback || !(0,
|
|
66
|
+
if (!callback || !(0, _utils2.isFunction)(callback) || callback.params.length !== 1 + callbackArgIndex) {
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
const argument = callback.params[callbackArgIndex];
|
|
71
71
|
|
|
72
|
-
if (argument.type !==
|
|
72
|
+
if (argument.type !== _utils.AST_NODE_TYPES.Identifier) {
|
|
73
73
|
context.report({
|
|
74
74
|
node: argument,
|
|
75
75
|
messageId: 'noDoneCallback'
|
|
@@ -126,7 +126,7 @@ var _default = (0, _utils.createRule)({
|
|
|
126
126
|
let afterReplacement = ')';
|
|
127
127
|
let replaceBefore = true;
|
|
128
128
|
|
|
129
|
-
if (body.type ===
|
|
129
|
+
if (body.type === _utils.AST_NODE_TYPES.BlockStatement) {
|
|
130
130
|
const keyword = 'return';
|
|
131
131
|
beforeReplacement = `${keyword} ${beforeReplacement}{`;
|
|
132
132
|
afterReplacement += '}';
|