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
fix(valid-expect): validate async expect calls #78
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,72 @@ | |
* MIT license, Tom Vincent. | ||
*/ | ||
|
||
const getDocsUrl = require('./util').getDocsUrl; | ||
const get = require('lodash.get'); | ||
const util = require('./util'); | ||
|
||
const expectProperties = ['not', 'resolves', 'rejects']; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
url: util.getDocsUrl(__filename), | ||
}, | ||
}, | ||
create(context) { | ||
function validateAsyncExpects(node) { | ||
const callback = node.arguments[1]; | ||
if ( | ||
callback && | ||
util.isFunction(callback) && | ||
callback.body.type === 'BlockStatement' | ||
) { | ||
callback.body.body | ||
.filter(node => node.type === 'ExpressionStatement') | ||
.filter(node => { | ||
const objectName = get( | ||
node, | ||
'expression.callee.object.object.callee.name' | ||
); | ||
const propertyName = get( | ||
node, | ||
'expression.callee.object.property.name' | ||
); | ||
|
||
return ( | ||
node.expression.type === 'CallExpression' && | ||
objectName === 'expect' && | ||
(propertyName === 'resolves' || propertyName === 'rejects') | ||
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 wonder if we can reuse the logic somehow with the other 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. Good idea. I'll look into that while resolving the failing test case you reported below. |
||
); | ||
}) | ||
.forEach(node => { | ||
const propertyName = get( | ||
node, | ||
'expression.callee.object.property.name' | ||
); | ||
const isAwaitInsideExpect = | ||
get(node, 'expression.callee.object.object.arguments[0].type') === | ||
'AwaitExpression'; | ||
|
||
context.report({ | ||
node, | ||
message: isAwaitInsideExpect | ||
? "Cannot use '{{ propertyName }}' with an awaited expect expression" | ||
: callback.async | ||
? "Must await 'expect.{{ propertyName }}' statement" | ||
: "Must return or await 'expect.{{ propertyName }}' statement", | ||
data: { propertyName }, | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
CallExpression(node) { | ||
if (util.isTestCase(node)) { | ||
validateAsyncExpects(node); | ||
return; | ||
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. If we are in a test case function, there is no need to proceed, since the |
||
} | ||
|
||
const calleeName = node.callee.name; | ||
|
||
if (calleeName === 'expect') { | ||
|
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.
can you assert on the locations as well?
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.
Yep, will make those changes 👍