eslint-plugin-jest 25.7.0 → 26.1.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 +2 -1
- package/docs/rules/no-conditional-in-test.md +79 -0
- package/docs/rules/no-if.md +5 -0
- package/docs/rules/prefer-snapshot-hint.md +188 -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 +13 -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 +10 -10
- package/lib/rules/prefer-equality-matcher.js +10 -10
- package/lib/rules/prefer-expect-assertions.js +12 -12
- package/lib/rules/prefer-expect-resolves.js +4 -4
- package/lib/rules/prefer-snapshot-hint.js +112 -0
- 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 +18 -18
- package/lib/rules/valid-title.js +14 -14
- package/package.json +9 -7
- package/CHANGELOG.md +0 -846
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][] |
|
|
@@ -166,7 +167,6 @@ installations requiring long-term consistency.
|
|
|
166
167
|
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![suggest][] |
|
|
167
168
|
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
|
|
168
169
|
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
|
|
169
|
-
| [no-if](docs/rules/no-if.md) | Disallow conditional logic | | |
|
|
170
170
|
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
|
|
171
171
|
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
|
|
172
172
|
| [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
|
|
@@ -183,6 +183,7 @@ installations requiring long-term consistency.
|
|
|
183
183
|
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
|
|
184
184
|
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
|
|
185
185
|
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | ![fixable][] |
|
|
186
|
+
| [prefer-snapshot-hint](docs/rules/prefer-snapshot-hint.md) | Prefer including a hint with external snapshots | | |
|
|
186
187
|
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
|
|
187
188
|
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
|
|
188
189
|
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using `toBe()` for primitive literals | ![style][] | ![fixable][] |
|
|
@@ -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
|
+
```
|
package/docs/rules/no-if.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Disallow conditional logic (`no-if`)
|
|
2
2
|
|
|
3
|
+
## Deprecated
|
|
4
|
+
|
|
5
|
+
This rule has been deprecated in favor of
|
|
6
|
+
[`no-conditional-in-test`](no-conditional-in-test.md).
|
|
7
|
+
|
|
3
8
|
Conditional logic in tests is usually an indication that a test is attempting to
|
|
4
9
|
cover too much, and not testing the logic it intends to. Each branch of code
|
|
5
10
|
executing within an if statement will usually be better served by a test devoted
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# Prefer including a hint with external snapshots (`prefer-snapshot-hint`)
|
|
2
|
+
|
|
3
|
+
When working with external snapshot matchers it's considered best practice to
|
|
4
|
+
provide a hint (as the last argument to the matcher) describing the expected
|
|
5
|
+
snapshot content that will be included in the snapshots name by Jest.
|
|
6
|
+
|
|
7
|
+
This makes it easier for reviewers to verify the snapshots during review, and
|
|
8
|
+
for anyone to know whether an outdated snapshot is the correct behavior before
|
|
9
|
+
updating.
|
|
10
|
+
|
|
11
|
+
## Rule details
|
|
12
|
+
|
|
13
|
+
This rule looks for any use of an external snapshot matcher (e.g.
|
|
14
|
+
`toMatchSnapshot` and `toThrowErrorMatchingSnapshot`) and checks if they include
|
|
15
|
+
a snapshot hint.
|
|
16
|
+
|
|
17
|
+
## Options
|
|
18
|
+
|
|
19
|
+
### `'always'`
|
|
20
|
+
|
|
21
|
+
Require a hint to _always_ be provided when using external snapshot matchers.
|
|
22
|
+
|
|
23
|
+
Examples of **incorrect** code for the `'always'` option:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const snapshotOutput = ({ stdout, stderr }) => {
|
|
27
|
+
expect(stdout).toMatchSnapshot();
|
|
28
|
+
expect(stderr).toMatchSnapshot();
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe('cli', () => {
|
|
32
|
+
describe('--version flag', () => {
|
|
33
|
+
it('prints the version', async () => {
|
|
34
|
+
snapshotOutput(await runCli(['--version']));
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('--config flag', () => {
|
|
39
|
+
it('reads the config', async () => {
|
|
40
|
+
const { stdout, parsedConfig } = await runCli([
|
|
41
|
+
'--config',
|
|
42
|
+
'jest.config.js',
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
expect(stdout).toMatchSnapshot();
|
|
46
|
+
expect(parsedConfig).toMatchSnapshot();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('prints nothing to stderr', async () => {
|
|
50
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
51
|
+
|
|
52
|
+
expect(stderr).toMatchSnapshot();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('when the file does not exist', () => {
|
|
56
|
+
it('throws an error', async () => {
|
|
57
|
+
await expect(
|
|
58
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
59
|
+
).rejects.toThrowErrorMatchingSnapshot();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Examples of **correct** code for the `'always'` option:
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
const snapshotOutput = ({ stdout, stderr }, hints) => {
|
|
70
|
+
expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`);
|
|
71
|
+
expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
describe('cli', () => {
|
|
75
|
+
describe('--version flag', () => {
|
|
76
|
+
it('prints the version', async () => {
|
|
77
|
+
snapshotOutput(await runCli(['--version']), {
|
|
78
|
+
stdout: 'version string',
|
|
79
|
+
stderr: 'empty',
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('--config flag', () => {
|
|
85
|
+
it('reads the config', async () => {
|
|
86
|
+
const { stdout } = await runCli(['--config', 'jest.config.js']);
|
|
87
|
+
|
|
88
|
+
expect(stdout).toMatchSnapshot({}, 'stdout: config settings');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('prints nothing to stderr', async () => {
|
|
92
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
93
|
+
|
|
94
|
+
expect(stderr).toMatchInlineSnapshot();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('when the file does not exist', () => {
|
|
98
|
+
it('throws an error', async () => {
|
|
99
|
+
await expect(
|
|
100
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
101
|
+
).rejects.toThrowErrorMatchingSnapshot('stderr: config error');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### `'multi'` (default)
|
|
109
|
+
|
|
110
|
+
Require a hint to be provided when there are multiple external snapshot matchers
|
|
111
|
+
within the scope (meaning it includes nested calls).
|
|
112
|
+
|
|
113
|
+
Examples of **incorrect** code for the `'multi'` option:
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
const snapshotOutput = ({ stdout, stderr }) => {
|
|
117
|
+
expect(stdout).toMatchSnapshot();
|
|
118
|
+
expect(stderr).toMatchSnapshot();
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
describe('cli', () => {
|
|
122
|
+
describe('--version flag', () => {
|
|
123
|
+
it('prints the version', async () => {
|
|
124
|
+
snapshotOutput(await runCli(['--version']));
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('--config flag', () => {
|
|
129
|
+
it('reads the config', async () => {
|
|
130
|
+
const { stdout, parsedConfig } = await runCli([
|
|
131
|
+
'--config',
|
|
132
|
+
'jest.config.js',
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
expect(stdout).toMatchSnapshot();
|
|
136
|
+
expect(parsedConfig).toMatchSnapshot();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('prints nothing to stderr', async () => {
|
|
140
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
141
|
+
|
|
142
|
+
expect(stderr).toMatchSnapshot();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Examples of **correct** code for the `'multi'` option:
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
const snapshotOutput = ({ stdout, stderr }, hints) => {
|
|
152
|
+
expect(stdout).toMatchSnapshot({}, `stdout: ${hints.stdout}`);
|
|
153
|
+
expect(stderr).toMatchSnapshot({}, `stderr: ${hints.stderr}`);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
describe('cli', () => {
|
|
157
|
+
describe('--version flag', () => {
|
|
158
|
+
it('prints the version', async () => {
|
|
159
|
+
snapshotOutput(await runCli(['--version']), {
|
|
160
|
+
stdout: 'version string',
|
|
161
|
+
stderr: 'empty',
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('--config flag', () => {
|
|
167
|
+
it('reads the config', async () => {
|
|
168
|
+
const { stdout } = await runCli(['--config', 'jest.config.js']);
|
|
169
|
+
|
|
170
|
+
expect(stdout).toMatchSnapshot();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('prints nothing to stderr', async () => {
|
|
174
|
+
const { stderr } = await runCli(['--config', 'jest.config.js']);
|
|
175
|
+
|
|
176
|
+
expect(stderr).toMatchInlineSnapshot();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('when the file does not exist', () => {
|
|
180
|
+
it('throws an error', async () => {
|
|
181
|
+
await expect(
|
|
182
|
+
runCli(['--config', 'does-not-exist.js']),
|
|
183
|
+
).rejects.toThrowErrorMatchingSnapshot();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
```
|
|
@@ -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
|
|