Skip to content

Commit

Permalink
feat: create no-conditional-in-test rule (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Feb 6, 2022
1 parent 159d109 commit 004ddc5
Show file tree
Hide file tree
Showing 6 changed files with 1,156 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ installations requiring long-term consistency.
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
| [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests | | |
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
| [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
Expand Down
79 changes: 79 additions & 0 deletions docs/rules/no-conditional-in-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Disallow conditional logic in tests (`no-conditional-in-test`)

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 a conditional statement will usually be better served by a test
devoted to it.

## Rule Details

This rule reports on any use of a conditional statement such as `if`, `switch`,
and ternary expressions.

Examples of **incorrect** code for this rule:

```js
it('foo', () => {
if (true) {
doTheThing();
}
});

it('bar', () => {
switch (mode) {
case 'none':
generateNone();
case 'single':
generateOne();
case 'multiple':
generateMany();
}

expect(fixtures.length).toBeGreaterThan(-1);
});

it('baz', async () => {
const promiseValue = () => {
return something instanceof Promise
? something
: Promise.resolve(something);
};

await expect(promiseValue()).resolves.toBe(1);
});
```

Examples of **correct** code for this rule:

```js
describe('my tests', () => {
if (true) {
it('foo', () => {
doTheThing();
});
}
});

beforeEach(() => {
switch (mode) {
case 'none':
generateNone();
case 'single':
generateOne();
case 'multiple':
generateMany();
}
});

it('bar', () => {
expect(fixtures.length).toBeGreaterThan(-1);
});

const promiseValue = something => {
return something instanceof Promise ? something : Promise.resolve(something);
};

it('baz', async () => {
await expect(promiseValue()).resolves.toBe(1);
});
```
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/rules.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Object {
"jest/no-alias-methods": "error",
"jest/no-commented-out-tests": "error",
"jest/no-conditional-expect": "error",
"jest/no-conditional-in-test": "error",
"jest/no-deprecated-functions": "error",
"jest/no-disabled-tests": "error",
"jest/no-done-callback": "error",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import { resolve } from 'path';
import plugin from '../';

const numberOfRules = 45;
const numberOfRules = 46;
const ruleNames = Object.keys(plugin.rules);
const deprecatedRules = Object.entries(plugin.rules)
.filter(([, rule]) => rule.meta.deprecated)
Expand Down
Loading

0 comments on commit 004ddc5

Please sign in to comment.