Skip to content

Commit aba53e4

Browse files
committed
feat: create no-conditional-expect rule
1 parent 054cd24 commit aba53e4

File tree

6 files changed

+600
-1
lines changed

6 files changed

+600
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ installations requiring long-term consistency.
134134
| [lowercase-name](docs/rules/lowercase-name.md) | Enforce lowercase test names | | ![fixable][] |
135135
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ![style][] | ![fixable][] |
136136
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
137+
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | | |
137138
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | | ![fixable][] |
138139
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
139140
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |

docs/rules/no-conditional-expect.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Prevent calling `expect` conditionally (`no-conditional-expect`)
2+
3+
This rule prevents the use of `expect` in conditional blocks, such as `if`s &
4+
`catch`s.
5+
6+
## Rule Details
7+
8+
Jest considered a test to have failed if it throws an error, rather than on if
9+
any particular function is called, meaning conditional calls to `expect` could
10+
result in tests silently being skipped.
11+
12+
Additionally, conditionals tend to make tests more brittle and complex, as they
13+
increase the amount of mental thinking needed to understand what is actually
14+
being tested.
15+
16+
While `expect.assertions` & `expect.hasAssertions` can help prevent tests from
17+
silently being skipped, when combined with conditionals they typically result in
18+
even more complexity being introduced.
19+
20+
The following patterns are warnings:
21+
22+
```js
23+
it('foo', () => {
24+
doTest && expect(1).toBe(2);
25+
});
26+
27+
it('bar', () => {
28+
if (!skipTest) {
29+
expect(1).toEqual(2);
30+
}
31+
});
32+
33+
it('baz', async () => {
34+
try {
35+
await foo();
36+
} catch (err) {
37+
expect(err).toMatchObject({ code: 'MODULE_NOT_FOUND' });
38+
}
39+
});
40+
```
41+
42+
The following patterns are not warnings:
43+
44+
```js
45+
it('foo', () => {
46+
expect(!value).toBe(false);
47+
});
48+
49+
function getValue() {
50+
if (process.env.FAIL) {
51+
return 1;
52+
}
53+
54+
return 2;
55+
}
56+
57+
it('foo', () => {
58+
expect(getValue()).toBe(2);
59+
});
60+
61+
it('validates the request', () => {
62+
try {
63+
processRequest(request);
64+
} catch {
65+
// ignore errors
66+
} finally {
67+
expect(validRequest).toHaveBeenCalledWith(request);
68+
}
69+
});
70+
```

src/__tests__/__snapshots__/rules.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Object {
1515
"jest/lowercase-name": "error",
1616
"jest/no-alias-methods": "error",
1717
"jest/no-commented-out-tests": "error",
18+
"jest/no-conditional-expect": "error",
1819
"jest/no-deprecated-functions": "error",
1920
"jest/no-disabled-tests": "error",
2021
"jest/no-duplicate-hooks": "error",

src/__tests__/rules.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { resolve } from 'path';
33
import plugin from '../';
44

55
const ruleNames = Object.keys(plugin.rules);
6-
const numberOfRules = 42;
6+
const numberOfRules = 43;
77

88
describe('rules', () => {
99
it('should have a corresponding doc for each rule', () => {

0 commit comments

Comments
 (0)