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

chore: add some comments for valid-expect #353

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/rules/valid-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ export default {
arrayExceptions[key] = true;
};

/**
* Promise method that accepts an array of promises,
* ( eg. Promise.all), will throw warnings for the each
* unawaited or non-returned promise. To avoid throwing
* multiple warnings, we check if there is a warning in
* the given location.
*/
const promiseArrayExceptionExists = loc => {
const key = promiseArrayExceptionKey(loc);
return !!arrayExceptions[key];
Expand Down Expand Up @@ -234,13 +241,23 @@ export default {
) {
let parentNode = getClosestParentCallExpressionNode(node);
if (parentNode) {
const { options } = context;
const allowReturn = !options[0] || !options[0].alwaysAwait;
/**
* If parent node is an array expression, we'll report the warning,
* for the array object, not for each individual assertion.
*/
const isParentArrayExpression =
parentNode.parent.type === 'ArrayExpression';

const { options } = context;
const allowReturn = !options[0] || !options[0].alwaysAwait;
const orReturned = allowReturn ? ' or returned' : '';
let messageId = 'asyncMustBeAwaited';

/**
* An async assertion can be chained with `then` or `catch` statements.
* In that case our target CallExpression node is the one with
* the last `then` or `catch` statement.
*/
parentNode = getParentIfThenified(parentNode);

// Promise.x([expect()]) || Promise.x(expect())
Expand All @@ -256,7 +273,9 @@ export default {
}

if (
// If node is not awaited or returned
!checkIfValidReturn(parentNode.parent, allowReturn) &&
// if we didn't warn user already
!promiseArrayExceptionExists(parentNode.loc)
) {
context.report({
Expand Down