Skip to content

Commit eb31a54

Browse files
tushardholeSimenB
authored andcommitted
feat: implement valid-expect-in-promise rule (#42)
* feat: implement valid-expect-in-promise rule introduce a lint rule to report error when testing promises. If a expectation is added inside a then or catch block of promise then as per jest docs, it is mandatory to return that promise for succesfull execution of that expectation. This rule will report all such expectations where promise is not returned. * fix: do not add the rule to recommended * fix: do not validate await expressions for valid_expect_in_promise * fix: scenario with expect in nested then/catch * docs: updating docs for valid-expect-in-promise rule * test: add failing test case * fix: scenario where promise is returned later * fix: adding more scenarios where promise is returned later * test: adding more tests * fix: adding scenarios for non test functions * fix: refactor to avoid multiple execution of getTestFuncBody * fix: scenario with arrow-short-hand-fn with implicit return statement * fix: scenario with multiline function in then block * fix: scenario with short hand arrow function in then block * fix: do not validate tests with done async param * fix: scenario where expect in then is preceded by return * fix: duplicate warning for same promise * fix: better naming * test: better formatting
1 parent b44813a commit eb31a54

File tree

5 files changed

+586
-0
lines changed

5 files changed

+586
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ for more information about extending configuration files.
8989
| [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using `toBeUndefined()` | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
9090
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
9191
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
92+
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Enforce having return statement when testing with promises | | |
9293

9394
## Credit
9495

docs/rules/valid-expect-in-promise.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Enforce having return statement when testing with promises (valid-expect-in-promise)
2+
3+
Ensure to return promise when having assertions in `then` or `catch` block of
4+
promise
5+
6+
## Rule details
7+
8+
This rule triggers a warning if,
9+
10+
* test is having assertions in `then` or `catch` block of a promise
11+
* and that promise is not returned from the test
12+
13+
### Default configuration
14+
15+
The following pattern is considered warning:
16+
17+
```js
18+
it('promise test', () => {
19+
somePromise.then(data => {
20+
expect(data).toEqual('foo');
21+
});
22+
});
23+
```
24+
25+
The following pattern is not warning:
26+
27+
```js
28+
it('promise test', () => {
29+
return somePromise.then(data => {
30+
expect(data).toEqual('foo');
31+
});
32+
});
33+
```

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const preferToBeUndefined = require('./rules/prefer_to_be_undefined');
99
const preferToHaveLength = require('./rules/prefer_to_have_length');
1010
const validExpect = require('./rules/valid_expect');
1111
const preferExpectAssertions = require('./rules/prefer_expect_assertions');
12+
const validExpectInPromise = require('./rules/valid_expect_in_promise');
1213

1314
const snapshotProcessor = require('./processors/snapshot-processor');
1415

@@ -64,5 +65,6 @@ module.exports = {
6465
'prefer-to-have-length': preferToHaveLength,
6566
'valid-expect': validExpect,
6667
'prefer-expect-assertions': preferExpectAssertions,
68+
'valid-expect-in-promise': validExpectInPromise,
6769
},
6870
};

0 commit comments

Comments
 (0)