Skip to content

Commit

Permalink
feat: implement valid-expect-in-promise rule (#42)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tushardhole authored and SimenB committed Jan 17, 2018
1 parent b44813a commit eb31a54
Show file tree
Hide file tree
Showing 5 changed files with 586 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ for more information about extending configuration files.
| [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using `toBeUndefined()` | | ![fixable](https://img.shields.io/badge/-fixable-green.svg) |
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended](https://img.shields.io/badge/-recommended-lightgrey.svg) | |
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Enforce having return statement when testing with promises | | |

## Credit

Expand Down
33 changes: 33 additions & 0 deletions docs/rules/valid-expect-in-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Enforce having return statement when testing with promises (valid-expect-in-promise)

Ensure to return promise when having assertions in `then` or `catch` block of
promise

## Rule details

This rule triggers a warning if,

* test is having assertions in `then` or `catch` block of a promise
* and that promise is not returned from the test

### Default configuration

The following pattern is considered warning:

```js
it('promise test', () => {
somePromise.then(data => {
expect(data).toEqual('foo');
});
});
```

The following pattern is not warning:

```js
it('promise test', () => {
return somePromise.then(data => {
expect(data).toEqual('foo');
});
});
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const preferToBeUndefined = require('./rules/prefer_to_be_undefined');
const preferToHaveLength = require('./rules/prefer_to_have_length');
const validExpect = require('./rules/valid_expect');
const preferExpectAssertions = require('./rules/prefer_expect_assertions');
const validExpectInPromise = require('./rules/valid_expect_in_promise');

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

Expand Down Expand Up @@ -64,5 +65,6 @@ module.exports = {
'prefer-to-have-length': preferToHaveLength,
'valid-expect': validExpect,
'prefer-expect-assertions': preferExpectAssertions,
'valid-expect-in-promise': validExpectInPromise,
},
};
Loading

0 comments on commit eb31a54

Please # to comment.