|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Line | |
|---|
| 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
|
|---|
| 9 | cases, using the 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.
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.