source: frontend/node_modules/eslint-plugin-jest/docs/rules/no-if.md

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
3Conditional logic in tests is usually an indication that a test is attempting to
4cover too much, and not testing the logic it intends to. Each branch of code
5executing within an if statement will usually be better served by a test devoted
6to it.
7
8Conditionals are often used to satisfy the typescript type checker. In these
9cases, using the non-null assertion operator (!) would be best.
10
11## Rule Details
12
13This rule prevents the use of if/ else statements and conditional (ternary)
14operations in tests.
15
16The following patterns are considered warnings:
17
18```js
19it('foo', () => {
20 if ('bar') {
21 // an if statement here is invalid
22 // you are probably testing too much
23 }
24});
25
26it('foo', () => {
27 const bar = foo ? 'bar' : null;
28});
29```
30
31These patterns would not be considered warnings:
32
33```js
34it('foo', () => {
35 // only test the 'foo' case
36});
37
38it('bar', () => {
39 // test the 'bar' case separately
40});
41
42it('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
52If you do not wish to prevent the use of if statements in tests, you can safely
53disable this rule.
Note: See TracBrowser for help on using the repository browser.