-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create
no-conditional-in-test
rule (#1027)
- Loading branch information
Showing
6 changed files
with
1,156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.