Skip to content

Commit d0a691c

Browse files
Matt Seccafiencartogram
Matt Seccafien
authored and
cartogram
committed
feat(rules): adds no-if rule
Update no-if.md
1 parent 31c7cef commit d0a691c

File tree

6 files changed

+517
-2
lines changed

6 files changed

+517
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ installations requiring long-term consistency.
117117
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
118118
| [no-hooks][] | Disallow setup and teardown hooks | | |
119119
| [no-identical-title][] | Disallow identical titles | ![recommended][] | |
120+
| [no-if][] | Disallow conditional logic | | |
120121
| [no-jasmine-globals][] | Disallow Jasmine globals | ![recommended][] | ![fixable-yellow][] |
121122
| [no-jest-import][] | Disallow importing `jest` | ![recommended][] | |
122123
| [no-mocks-import][] | Disallow manually importing from `__mocks__` | | |
@@ -165,6 +166,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting
165166
[no-focused-tests]: docs/rules/no-focused-tests.md
166167
[no-hooks]: docs/rules/no-hooks.md
167168
[no-identical-title]: docs/rules/no-identical-title.md
169+
[no-if]: docs/rules/no-if.md
168170
[no-jasmine-globals]: docs/rules/no-jasmine-globals.md
169171
[no-jest-import]: docs/rules/no-jest-import.md
170172
[no-mocks-import]: docs/rules/no-mocks-import.md

docs/rules/no-if.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.

src/__tests__/rules.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { resolve } from 'path';
33
import { rules } from '../';
44

55
const ruleNames = Object.keys(rules);
6-
const numberOfRules = 33;
6+
const numberOfRules = 34;
77

88
describe('rules', () => {
99
it('should have a corresponding doc for each rule', () => {

0 commit comments

Comments
 (0)