Skip to content

Commit e3d8ac5

Browse files
author
Matt Seccafien
committed
feat(rules): adds no-if rule
1 parent e901d03 commit e3d8ac5

File tree

5 files changed

+523
-0
lines changed

5 files changed

+523
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ installations requiring long-term consistency.
116116
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
117117
| [no-hooks][] | Disallow setup and teardown hooks | | |
118118
| [no-identical-title][] | Disallow identical titles | ![recommended][] | |
119+
| [no-if][] | Disallow conditional logic | | |
119120
| [no-jasmine-globals][] | Disallow Jasmine globals | ![recommended][] | ![fixable-yellow][] |
120121
| [no-jest-import][] | Disallow importing `jest` | ![recommended][] | |
121122
| [no-mocks-import][] | Disallow manually importing from `__mocks__` | | |
@@ -163,6 +164,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting
163164
[no-focused-tests]: docs/rules/no-focused-tests.md
164165
[no-hooks]: docs/rules/no-hooks.md
165166
[no-identical-title]: docs/rules/no-identical-title.md
167+
[no-if]: docs/rules/no-if.md
166168
[no-jasmine-globals]: docs/rules/no-jasmine-globals.md
167169
[no-jest-import]: docs/rules/no-jest-import.md
168170
[no-mocks-import]: docs/rules/no-mocks-import.md

docs/rules/no-if.md

Lines changed: 53 additions & 0 deletions
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 in to satisfy the typescript type checker when using
9+
a non-null assertion operator (!) would work.
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.

0 commit comments

Comments
 (0)