Skip to content

Commit

Permalink
fix(valid-expect-in-promise): support out of order awaits (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Oct 11, 2021
1 parent e731bce commit 07d2137
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/rules/__tests__/valid-expect-in-promise.test.ts
Expand Up @@ -383,6 +383,19 @@ ruleTester.run('valid-expect-in-promise', rule, {
}
);
`,
dedent`
it('is valid', async () => {
const promiseOne = loadNumber().then(number => {
expect(typeof number).toBe('number');
});
const promiseTwo = loadNumber().then(number => {
expect(typeof number).toBe('number');
});
await promiseTwo;
await promiseOne;
});
`,
dedent`
it("it1", () => somePromise.then(() => {
expect(someThing).toEqual(true)
Expand Down
7 changes: 5 additions & 2 deletions src/rules/valid-expect-in-promise.ts
Expand Up @@ -163,8 +163,11 @@ const isValueAwaitedOrReturned = (
}

if (node.type === AST_NODE_TYPES.ExpressionStatement) {
if (node.expression.type === AST_NODE_TYPES.AwaitExpression) {
return isPromiseMethodThatUsesValue(node.expression, identifier);
if (
node.expression.type === AST_NODE_TYPES.AwaitExpression &&
isPromiseMethodThatUsesValue(node.expression, identifier)
) {
return true;
}

// (re)assignment changes the runtime value, so if we've not found an
Expand Down

0 comments on commit 07d2137

Please sign in to comment.