Skip to content

Commit

Permalink
feat(rules): adds no-if rule
Browse files Browse the repository at this point in the history
Update no-if.md
  • Loading branch information
Matt Seccafien authored and cartogram committed Jul 17, 2019
1 parent 31c7cef commit 0d79e98
Show file tree
Hide file tree
Showing 6 changed files with 514 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ installations requiring long-term consistency.
| [no-focused-tests][] | Disallow focused tests | ![recommended][] | |
| [no-hooks][] | Disallow setup and teardown hooks | | |
| [no-identical-title][] | Disallow identical titles | ![recommended][] | |
| [no-if][] | Disallow conditional logic | | |
| [no-jasmine-globals][] | Disallow Jasmine globals | ![recommended][] | ![fixable-yellow][] |
| [no-jest-import][] | Disallow importing `jest` | ![recommended][] | |
| [no-mocks-import][] | Disallow manually importing from `__mocks__` | | |
Expand Down Expand Up @@ -165,6 +166,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting
[no-focused-tests]: docs/rules/no-focused-tests.md
[no-hooks]: docs/rules/no-hooks.md
[no-identical-title]: docs/rules/no-identical-title.md
[no-if]: docs/rules/no-if.md
[no-jasmine-globals]: docs/rules/no-jasmine-globals.md
[no-jest-import]: docs/rules/no-jest-import.md
[no-mocks-import]: docs/rules/no-mocks-import.md
Expand Down
53 changes: 53 additions & 0 deletions docs/rules/no-if.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Disallow conditional logic. (no-if)

Conditional logic in tests is usually an indication that a test is attempting to
cover too much, and not testing the logic it intends to. Each branch of code
executing within an if statement will usually be better served by a test devoted
to it.

Conditionals are often used to satisfy the typescript type checker. In these cases, using the
non-null assertion operator (!) would be best.

## Rule Details

This rule prevents the use of if/ else statements and conditional (ternary)
operations in tests.

The following patterns are considered warnings:

```js
it('foo', () => {
if ('bar') {
// an if statement here is invalid
// you are probably testing too much
}
});

it('foo', () => {
const bar = foo ? 'bar' : null;
});
```

These patterns would not be considered warnings:

```js
it('foo', () => {
// only test the 'foo' case
});

it('bar', () => {
// test the 'bar' case separately
});

it('foo', () => {
function foo(bar) {
// nested functions are valid
return foo ? bar : null;
}
});
```

## When Not To Use It

If you do not wish to prevent the use of if statements in tests, you can safely
disable this rule.
2 changes: 1 addition & 1 deletion src/__tests__/rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { resolve } from 'path';
import { rules } from '../';

const ruleNames = Object.keys(rules);
const numberOfRules = 33;
const numberOfRules = 34;

describe('rules', () => {
it('should have a corresponding doc for each rule', () => {
Expand Down
Loading

0 comments on commit 0d79e98

Please sign in to comment.