Skip to content

Commit

Permalink
fix(valid-expect-in-promise): allow variables assigned awaited promises
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Oct 2, 2021
1 parent f794b0d commit a1b103c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/rules/__tests__/valid-expect-in-promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,17 @@ ruleTester.run('valid-expect-in-promise', rule, {
return promise;
});
`,
dedent`
it('is a test', async () => {
const value = await somePromise().then(response => {
expect(response).toHaveProperty('data');
return response.data;
});
expect(value).toBe('hello world');
});
`,
dedent`
it(
'test function',
Expand Down
4 changes: 4 additions & 0 deletions src/rules/valid-expect-in-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ const isVariableAwaitedOrReturned = (
const [variable] = variables.declarations;
const name = getVariableName(variable);

if (variable.init?.type === AST_NODE_TYPES.AwaitExpression) {
return true;
}

// null means that the variable is destructured, which is pretty much impossible
// for us to track, so we return true to bailout gracefully
if (name === null) {
Expand Down

0 comments on commit a1b103c

Please sign in to comment.