Skip to content

Commit

Permalink
fix(valid-expect-in-promise): support deep property call chains
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Oct 11, 2021
1 parent 585dde6 commit 7627d57
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
42 changes: 42 additions & 0 deletions src/rules/__tests__/valid-expect-in-promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,48 @@ ruleTester.run('valid-expect-in-promise', rule, {
return number + 1;
});
expect(await promise).resolves.toBeGreaterThan(1);
});
`,
dedent`
it('is valid', async () => {
const promise = loadNumber().then(number => {
expect(typeof number).toBe('number');
return number + 1;
});
expect(1).toBeGreaterThan(await promise);
});
`,
dedent`
it('is valid', async () => {
const promise = loadNumber().then(number => {
expect(typeof number).toBe('number');
return number + 1;
});
expect.this.that.is(await promise);
});
`,
dedent`
it('is valid', async () => {
expect(await loadNumber().then(number => {
expect(typeof number).toBe('number');
return number + 1;
})).toBeGreaterThan(1);
});
`,
dedent`
it('is valid', async () => {
const promise = loadNumber().then(number => {
expect(typeof number).toBe('number');
return number + 1;
});
logValue(await promise);
});
`,
Expand Down
33 changes: 20 additions & 13 deletions src/rules/valid-expect-in-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,29 @@ const isPromiseMethodThatUsesValue = (
*/
const isValueAwaitedInArguments = (
name: string,
node: TSESTree.CallExpression,
call: TSESTree.CallExpression,
): boolean => {
for (const argument of node.arguments) {
if (
argument.type === AST_NODE_TYPES.AwaitExpression &&
isIdentifier(argument.argument, name)
) {
return true;
let node: TSESTree.Node = call;

while (node) {
if (node.type === AST_NODE_TYPES.CallExpression) {
for (const argument of node.arguments) {
if (
argument.type === AST_NODE_TYPES.AwaitExpression &&
isIdentifier(argument.argument, name)
) {
return true;
}
}

node = node.callee;
}
}

if (
node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.type === AST_NODE_TYPES.CallExpression
) {
return isValueAwaitedInArguments(name, node.callee.object);
if (node.type !== AST_NODE_TYPES.MemberExpression) {
break;
}

node = node.object;
}

return false;
Expand Down

0 comments on commit 7627d57

Please sign in to comment.