Skip to content

Commit 1f92185

Browse files
yatkiSimenB
authored andcommitted
chore: add some comments for valid-expect (#353)
1 parent 0bbfb3a commit 1f92185

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/rules/valid-expect.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ export default {
132132
arrayExceptions[key] = true;
133133
};
134134

135+
/**
136+
* Promise method that accepts an array of promises,
137+
* ( eg. Promise.all), will throw warnings for the each
138+
* unawaited or non-returned promise. To avoid throwing
139+
* multiple warnings, we check if there is a warning in
140+
* the given location.
141+
*/
135142
const promiseArrayExceptionExists = loc => {
136143
const key = promiseArrayExceptionKey(loc);
137144
return !!arrayExceptions[key];
@@ -234,13 +241,23 @@ export default {
234241
) {
235242
let parentNode = getClosestParentCallExpressionNode(node);
236243
if (parentNode) {
237-
const { options } = context;
238-
const allowReturn = !options[0] || !options[0].alwaysAwait;
244+
/**
245+
* If parent node is an array expression, we'll report the warning,
246+
* for the array object, not for each individual assertion.
247+
*/
239248
const isParentArrayExpression =
240249
parentNode.parent.type === 'ArrayExpression';
250+
251+
const { options } = context;
252+
const allowReturn = !options[0] || !options[0].alwaysAwait;
241253
const orReturned = allowReturn ? ' or returned' : '';
242254
let messageId = 'asyncMustBeAwaited';
243255

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

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

258275
if (
276+
// If node is not awaited or returned
259277
!checkIfValidReturn(parentNode.parent, allowReturn) &&
278+
// if we didn't warn user already
260279
!promiseArrayExceptionExists(parentNode.loc)
261280
) {
262281
context.report({

0 commit comments

Comments
 (0)