-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Introduce a lint rule to report error when testing promises. If a exp… #42
Merged
SimenB
merged 19 commits into
jest-community:master
from
tushardhole:valid_expect_in_promise
Jan 17, 2018
Merged
Changes from 5 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 ab0f43c
fix: do not add the rule to recommended
tushardhole da393e0
fix: do not validate await expressions for valid_expect_in_promise
tushardhole dbba301
fix: scenario with expect in nested then/catch
tushardhole 4651b68
docs: updating docs for valid-expect-in-promise rule
tushardhole 5b25a69
test: add failing test case
SimenB 8b6367d
fix: scenario where promise is returned later
tushardhole ede8427
fix: adding more scenarios where promise is returned later
tushardhole 8614010
test: adding more tests
tushardhole 9abdd01
fix: adding scenarios for non test functions
tushardhole 3314dbf
fix: refactor to avoid multiple execution of getTestFuncBody
tushardhole 00422dd
fix: scenario with arrow-short-hand-fn with implicit return statement
tushardhole 1242897
fix: scenario with multiline function in then block
tushardhole 0bd4572
fix: scenario with short hand arrow function in then block
tushardhole 4237828
fix: do not validate tests with done async param
tushardhole 567b85c
fix: scenario where expect in then is preceded by return
tushardhole 5a69337
fix: duplicate warning for same promise
tushardhole 7e44766
fix: better naming
tushardhole e2dc02c
test: better formatting
tushardhole File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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'); | ||
}); | ||
}); | ||
``` |
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,152 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rules = require('../..').rules; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
}, | ||
}); | ||
|
||
const expectedMsg = | ||
'Promise should be returned to test its fulfillment or rejection'; | ||
|
||
ruleTester.run('valid-expect-in-promise', rules['valid-expect-in-promise'], { | ||
invalid: [ | ||
{ | ||
code: | ||
'it("it1", () => { somePromise.then(' + | ||
'() => {expect(someThing).toEqual(true)})})', | ||
errors: [ | ||
{ | ||
column: 19, | ||
endColumn: 76, | ||
message: expectedMsg, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'it("it1", function() { getSomeThing().getPromise().then(' + | ||
'function() {expect(someThing).toEqual(true)})})', | ||
errors: [ | ||
{ | ||
column: 24, | ||
endColumn: 102, | ||
message: expectedMsg, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'it("it1", function() { Promise.resolve().then(' + | ||
'function() {expect(someThing).toEqual(true)})})', | ||
errors: [ | ||
{ | ||
column: 24, | ||
endColumn: 92, | ||
message: expectedMsg, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'it("it1", function() { somePromise.catch(' + | ||
'function() {expect(someThing).toEqual(true)})})', | ||
errors: [ | ||
{ | ||
column: 24, | ||
endColumn: 87, | ||
message: expectedMsg, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'it("it1", function() { somePromise.then(' + | ||
'function() { expect(someThing).toEqual(true)})})', | ||
errors: [ | ||
{ | ||
column: 24, | ||
endColumn: 87, | ||
message: expectedMsg, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'it("it1", function() { Promise.resolve().then(' + | ||
'function() { /*fulfillment*/ expect(someThing).toEqual(true)}, ' + | ||
'function() { /*rejection*/ expect(someThing).toEqual(true)})})', | ||
errors: [ | ||
{ | ||
column: 24, | ||
endColumn: 170, | ||
message: expectedMsg, | ||
}, | ||
{ | ||
column: 24, | ||
endColumn: 170, | ||
message: expectedMsg, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
'it("it1", function() { Promise.resolve().then(' + | ||
'function() { /*fulfillment*/}, ' + | ||
'function() { /*rejection*/ expect(someThing).toEqual(true)})})', | ||
errors: [ | ||
{ | ||
column: 24, | ||
endColumn: 138, | ||
message: expectedMsg, | ||
}, | ||
], | ||
}, | ||
], | ||
|
||
valid: [ | ||
'it("it1", () => { return somePromise.then(() => {expect(someThing).toEqual(true)})})', | ||
|
||
'it("it1", function() { return somePromise.catch(' + | ||
'function() {expect(someThing).toEqual(true)})})', | ||
|
||
'it("it1", function() { somePromise.then(' + | ||
'function() {doSomeThingButNotExpect()})})', | ||
|
||
'it("it1", function() { return getSomeThing().getPromise().then(' + | ||
'function() {expect(someThing).toEqual(true)})})', | ||
|
||
'it("it1", function() { return Promise.resolve().then(' + | ||
'function() {expect(someThing).toEqual(true)})})', | ||
|
||
'it("it1", function() { return Promise.resolve().then(' + | ||
'function() { /*fulfillment*/ expect(someThing).toEqual(true)}, ' + | ||
'function() { /*rejection*/ expect(someThing).toEqual(true)})})', | ||
|
||
'it("it1", function() { return Promise.resolve().then(' + | ||
'function() { /*fulfillment*/}, ' + | ||
'function() { /*rejection*/ expect(someThing).toEqual(true)})})', | ||
|
||
'it("it1", function() { return somePromise.then()})', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add assertions that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will work on that. |
||
|
||
'it("it1", async () => { await Promise.resolve().then(' + | ||
'function() {expect(someThing).toEqual(true)})})', | ||
|
||
'it("it1", async () => ' + | ||
'{ await somePromise.then(() => {expect(someThing).toEqual(true)})})', | ||
|
||
'it("it1", async () => { await getSomeThing().getPromise().then(' + | ||
'function() {expect(someThing).toEqual(true)})})', | ||
|
||
"it('it1', () => {return somePromise." + | ||
'then(() => {expect(someThing).toEqual(true);})' + | ||
'.then(() => {expect(someThing).toEqual(true);})});', | ||
|
||
"it('it1', () => {return somePromise." + | ||
'then(() => {expect(someThing).toEqual(true);})' + | ||
'.catch(() => {expect(someThing).toEqual(false);})});', | ||
], | ||
}); |
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,97 @@ | ||
'use strict'; | ||
|
||
const reportMsg = | ||
'Promise should be returned to test its fulfillment or rejection'; | ||
|
||
const isThenOrCatch = node => { | ||
return node.property.name == 'then' || node.property.name == 'catch'; | ||
}; | ||
|
||
const isFunction = type => { | ||
return type == 'FunctionExpression' || type == 'ArrowFunctionExpression'; | ||
}; | ||
|
||
const isBodyCallExpression = argumentBody => { | ||
try { | ||
return argumentBody.body[0].expression.type == 'CallExpression'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
const isExpectCall = calleeObject => { | ||
try { | ||
return calleeObject.callee.name == 'expect'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
const reportReturnRequired = (context, node) => { | ||
context.report({ | ||
loc: { | ||
end: { | ||
column: node.parent.parent.loc.end.column, | ||
line: node.parent.parent.loc.end.line, | ||
}, | ||
start: node.parent.parent.loc.start, | ||
}, | ||
message: reportMsg, | ||
node, | ||
}); | ||
}; | ||
|
||
const isParentThenOrReturn = node => { | ||
try { | ||
return ( | ||
node.parent.parent.type == 'ReturnStatement' || | ||
node.parent.parent.property.name === 'then' || | ||
node.parent.parent.property.name === 'catch' | ||
); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
const verifyExpectWithReturn = (argument, node, context) => { | ||
if ( | ||
argument && | ||
isFunction(argument.type) && | ||
isBodyCallExpression(argument.body) | ||
) { | ||
const calleeInThenOrCatch = argument.body.body[0].expression.callee.object; | ||
if (isExpectCall(calleeInThenOrCatch)) { | ||
if (!isParentThenOrReturn(node)) { | ||
reportReturnRequired(context, node); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const isAwaitExpression = node => { | ||
return node.parent.parent && node.parent.parent.type == 'AwaitExpression'; | ||
}; | ||
|
||
module.exports = context => { | ||
return { | ||
MemberExpression(node) { | ||
if ( | ||
node.type == 'MemberExpression' && | ||
isThenOrCatch(node) && | ||
node.parent.type == 'CallExpression' && | ||
!isAwaitExpression(node) | ||
) { | ||
const parent = node.parent; | ||
const arg1 = parent.arguments[0]; | ||
const arg2 = parent.arguments[1]; | ||
|
||
// then block can have two args, fulfillment & rejection | ||
// then block can have one args, fulfillment | ||
// catch block can have one args, rejection | ||
// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise | ||
verifyExpectWithReturn(arg1, node, context); | ||
verifyExpectWithReturn(arg2, node, context); | ||
} | ||
}, | ||
}; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this 2 errors? It should just be one, there is only a single promise here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will fix that.