File tree 6 files changed +517
-2
lines changed
6 files changed +517
-2
lines changed Original file line number Diff line number Diff line change @@ -117,6 +117,7 @@ installations requiring long-term consistency.
117
117
| [ no-focused-tests] [ ] | Disallow focused tests | ![ recommended] [ ] | |
118
118
| [ no-hooks] [ ] | Disallow setup and teardown hooks | | |
119
119
| [ no-identical-title] [ ] | Disallow identical titles | ![ recommended] [ ] | |
120
+ | [ no-if] [ ] | Disallow conditional logic | | |
120
121
| [ no-jasmine-globals] [ ] | Disallow Jasmine globals | ![ recommended] [ ] | ![ fixable-yellow] [ ] |
121
122
| [ no-jest-import] [ ] | Disallow importing ` jest ` | ![ recommended] [ ] | |
122
123
| [ no-mocks-import] [ ] | Disallow manually importing from ` __mocks__ ` | | |
@@ -165,6 +166,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting
165
166
[ no-focused-tests ] : docs/rules/no-focused-tests.md
166
167
[ no-hooks ] : docs/rules/no-hooks.md
167
168
[ no-identical-title ] : docs/rules/no-identical-title.md
169
+ [ no-if ] : docs/rules/no-if.md
168
170
[ no-jasmine-globals ] : docs/rules/no-jasmine-globals.md
169
171
[ no-jest-import ] : docs/rules/no-jest-import.md
170
172
[ no-mocks-import ] : docs/rules/no-mocks-import.md
Original file line number Diff line number Diff line change
1
+ # Disallow conditional logic. (no-if)
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 an if statement will usually be better served by a test devoted
6
+ to it.
7
+
8
+ Conditionals are often used to satisfy the typescript type checker. In these cases, using the
9
+ non-null assertion operator (!) would be best.
10
+
11
+ ## Rule Details
12
+
13
+ This rule prevents the use of if/ else statements and conditional (ternary)
14
+ operations in tests.
15
+
16
+ The following patterns are considered warnings:
17
+
18
+ ``` js
19
+ it (' foo' , () => {
20
+ if (' bar' ) {
21
+ // an if statement here is invalid
22
+ // you are probably testing too much
23
+ }
24
+ });
25
+
26
+ it (' foo' , () => {
27
+ const bar = foo ? ' bar' : null ;
28
+ });
29
+ ```
30
+
31
+ These patterns would not be considered warnings:
32
+
33
+ ``` js
34
+ it (' foo' , () => {
35
+ // only test the 'foo' case
36
+ });
37
+
38
+ it (' bar' , () => {
39
+ // test the 'bar' case separately
40
+ });
41
+
42
+ it (' foo' , () => {
43
+ function foo (bar ) {
44
+ // nested functions are valid
45
+ return foo ? bar : null ;
46
+ }
47
+ });
48
+ ```
49
+
50
+ ## When Not To Use It
51
+
52
+ If you do not wish to prevent the use of if statements in tests, you can safely
53
+ disable this rule.
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import { resolve } from 'path';
3
3
import { rules } from '../' ;
4
4
5
5
const ruleNames = Object . keys ( rules ) ;
6
- const numberOfRules = 33 ;
6
+ const numberOfRules = 34 ;
7
7
8
8
describe ( 'rules' , ( ) => {
9
9
it ( 'should have a corresponding doc for each rule' , ( ) => {
You can’t perform that action at this time.
0 commit comments