Skip to content

Commit d551850

Browse files
committed
feat: create no-conditional-in-test rule (#1027)
1 parent 159d109 commit d551850

File tree

6 files changed

+1156
-1
lines changed

6 files changed

+1156
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ installations requiring long-term consistency.
158158
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
159159
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
160160
| [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 | | |
161162
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
162163
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
163164
| [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |

docs/rules/no-conditional-in-test.md

+79
Original file line numberDiff line numberDiff line change
@@ -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+
```

src/__tests__/__snapshots__/rules.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Object {
1616
"jest/no-alias-methods": "error",
1717
"jest/no-commented-out-tests": "error",
1818
"jest/no-conditional-expect": "error",
19+
"jest/no-conditional-in-test": "error",
1920
"jest/no-deprecated-functions": "error",
2021
"jest/no-disabled-tests": "error",
2122
"jest/no-done-callback": "error",

src/__tests__/rules.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync } from 'fs';
22
import { resolve } from 'path';
33
import plugin from '../';
44

5-
const numberOfRules = 45;
5+
const numberOfRules = 46;
66
const ruleNames = Object.keys(plugin.rules);
77
const deprecatedRules = Object.entries(plugin.rules)
88
.filter(([, rule]) => rule.meta.deprecated)

0 commit comments

Comments
 (0)