diff --git a/docs/rules/valid-expect.md b/docs/rules/valid-expect.md index ac8b19cb3..8d198730f 100644 --- a/docs/rules/valid-expect.md +++ b/docs/rules/valid-expect.md @@ -79,6 +79,7 @@ expect(); expect().toEqual('something'); expect('something', 'else'); expect('something'); +await expect('something'); expect(true).toBeDefined; expect(Promise.resolve('hello')).resolves; expect(Promise.resolve('hello')).resolves.toEqual('hello'); diff --git a/src/rules/__tests__/valid-expect.test.ts b/src/rules/__tests__/valid-expect.test.ts index ff0ccc837..b75b9b0eb 100644 --- a/src/rules/__tests__/valid-expect.test.ts +++ b/src/rules/__tests__/valid-expect.test.ts @@ -578,5 +578,11 @@ ruleTester.run('valid-expect', rule, { }, ], }, + { + code: `test("valid-expect", async () => { + await expect(Promise.resolve(1)); + });`, + errors: [{ endColumn: 41, column: 15, messageId: 'noAssertions' }], + }, ], }); diff --git a/src/rules/valid-expect.ts b/src/rules/valid-expect.ts index b1bde0ab1..c5a587981 100644 --- a/src/rules/valid-expect.ts +++ b/src/rules/valid-expect.ts @@ -90,6 +90,12 @@ const isAcceptableReturnNode = ( AST_NODE_TYPES.AwaitExpression, ].includes(node.type); +const isNoAssertionsParentNode = (node: TSESTree.Node): boolean => + node.type === AST_NODE_TYPES.ExpressionStatement || + (node.type === AST_NODE_TYPES.AwaitExpression && + node.parent !== undefined && + node.parent.type === AST_NODE_TYPES.ExpressionStatement); + const promiseArrayExceptionKey = ({ start, end }: TSESTree.SourceLocation) => `${start.line}:${start.column}-${end.line}:${end.column}`; @@ -280,10 +286,7 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({ // nothing called on "expect()" 'CallExpression:exit'(node: TSESTree.CallExpression) { - if ( - isExpectCall(node) && - node.parent.type === AST_NODE_TYPES.ExpressionStatement - ) { + if (isExpectCall(node) && isNoAssertionsParentNode(node.parent)) { context.report({ messageId: 'noAssertions', node }); } },