Skip to content

Commit da54732

Browse files
committed
fix(valid-expect-in-promise): allow variables assigned awaited promises
1 parent f794b0d commit da54732

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/rules/__tests__/valid-expect-in-promise.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,44 @@ ruleTester.run('valid-expect-in-promise', rule, {
272272
return promise;
273273
});
274274
`,
275+
dedent`
276+
it('is a test', async () => {
277+
const value = await somePromise().then(response => {
278+
expect(response).toHaveProperty('data');
279+
280+
return response.data;
281+
});
282+
283+
expect(value).toBe('hello world');
284+
});
285+
`,
286+
dedent`
287+
it('is a test', async () => {
288+
return await somePromise().then(response => {
289+
expect(response).toHaveProperty('data');
290+
291+
return response.data;
292+
});
293+
});
294+
`,
295+
dedent`
296+
it('is a test', async () => {
297+
return somePromise().then(response => {
298+
expect(response).toHaveProperty('data');
299+
300+
return response.data;
301+
});
302+
});
303+
`,
304+
dedent`
305+
it('is a test', async () => {
306+
await somePromise().then(response => {
307+
expect(response).toHaveProperty('data');
308+
309+
return response.data;
310+
});
311+
});
312+
`,
275313
dedent`
276314
it(
277315
'test function',

src/rules/valid-expect-in-promise.ts

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ const isVariableAwaitedOrReturned = (
138138
const [variable] = variables.declarations;
139139
const name = getVariableName(variable);
140140

141+
if (variable.init?.type === AST_NODE_TYPES.AwaitExpression) {
142+
return true;
143+
}
144+
141145
// null means that the variable is destructured, which is pretty much impossible
142146
// for us to track, so we return true to bailout gracefully
143147
if (name === null) {

0 commit comments

Comments
 (0)