Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create no-conditional-in-test rule #1027

Merged
merged 1 commit into from Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
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
@@ -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
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
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