Skip to content

Commit 8155a3d

Browse files
committed
feat(valid-expect): warn on await expect() with no assertions
1 parent 6ffb998 commit 8155a3d

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

docs/rules/valid-expect.md

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ expect();
7979
expect().toEqual('something');
8080
expect('something', 'else');
8181
expect('something');
82+
await expect('something');
8283
expect(true).toBeDefined;
8384
expect(Promise.resolve('hello')).resolves;
8485
expect(Promise.resolve('hello')).resolves.toEqual('hello');

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

+6
Original file line numberDiff line numberDiff line change
@@ -578,5 +578,11 @@ ruleTester.run('valid-expect', rule, {
578578
},
579579
],
580580
},
581+
{
582+
code: `test("valid-expect", async () => {
583+
await expect(Promise.resolve(1));
584+
});`,
585+
errors: [{ endColumn: 41, column: 15, messageId: 'noAssertions' }],
586+
},
581587
],
582588
});

src/rules/valid-expect.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ const isAcceptableReturnNode = (
9090
AST_NODE_TYPES.AwaitExpression,
9191
].includes(node.type);
9292

93+
const isNoAssertionsParentNode = (node: TSESTree.Node): boolean => {
94+
return (
95+
node.type === AST_NODE_TYPES.ExpressionStatement ||
96+
(node.type === AST_NODE_TYPES.AwaitExpression &&
97+
node.parent != null &&
98+
node.parent.type === AST_NODE_TYPES.ExpressionStatement)
99+
);
100+
};
101+
93102
const promiseArrayExceptionKey = ({ start, end }: TSESTree.SourceLocation) =>
94103
`${start.line}:${start.column}-${end.line}:${end.column}`;
95104

@@ -280,10 +289,7 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
280289

281290
// nothing called on "expect()"
282291
'CallExpression:exit'(node: TSESTree.CallExpression) {
283-
if (
284-
isExpectCall(node) &&
285-
node.parent.type === AST_NODE_TYPES.ExpressionStatement
286-
) {
292+
if (isExpectCall(node) && isNoAssertionsParentNode(node.parent)) {
287293
context.report({ messageId: 'noAssertions', node });
288294
}
289295
},

0 commit comments

Comments
 (0)