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

Introduce a lint rule to report error when testing promises. If a exp… #42

Merged
merged 19 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d70d9ed
feat: implement valid-expect-in-promise rule
tushardhole Dec 30, 2017
ab0f43c
fix: do not add the rule to recommended
tushardhole Jan 9, 2018
da393e0
fix: do not validate await expressions for valid_expect_in_promise
tushardhole Jan 9, 2018
dbba301
fix: scenario with expect in nested then/catch
tushardhole Jan 10, 2018
4651b68
docs: updating docs for valid-expect-in-promise rule
tushardhole Jan 10, 2018
5b25a69
test: add failing test case
SimenB Jan 14, 2018
8b6367d
fix: scenario where promise is returned later
tushardhole Jan 14, 2018
ede8427
fix: adding more scenarios where promise is returned later
tushardhole Jan 14, 2018
8614010
test: adding more tests
tushardhole Jan 14, 2018
9abdd01
fix: adding scenarios for non test functions
tushardhole Jan 14, 2018
3314dbf
fix: refactor to avoid multiple execution of getTestFuncBody
tushardhole Jan 14, 2018
00422dd
fix: scenario with arrow-short-hand-fn with implicit return statement
tushardhole Jan 14, 2018
1242897
fix: scenario with multiline function in then block
tushardhole Jan 15, 2018
0bd4572
fix: scenario with short hand arrow function in then block
tushardhole Jan 15, 2018
4237828
fix: do not validate tests with done async param
tushardhole Jan 15, 2018
567b85c
fix: scenario where expect in then is preceded by return
tushardhole Jan 15, 2018
5a69337
fix: duplicate warning for same promise
tushardhole Jan 16, 2018
7e44766
fix: better naming
tushardhole Jan 16, 2018
e2dc02c
test: better formatting
tushardhole Jan 17, 2018
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
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,
},
};