Skip to content

Commit 19798dd

Browse files
motiz88G-Rath
authored andcommitted
feat(valid-expect): warn on await expect() with no assertions (#496)
1 parent 6ffb998 commit 19798dd

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-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

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

93+
const isNoAssertionsParentNode = (node: TSESTree.Node): boolean =>
94+
node.type === AST_NODE_TYPES.ExpressionStatement ||
95+
(node.type === AST_NODE_TYPES.AwaitExpression &&
96+
node.parent !== undefined &&
97+
node.parent.type === AST_NODE_TYPES.ExpressionStatement);
98+
9399
const promiseArrayExceptionKey = ({ start, end }: TSESTree.SourceLocation) =>
94100
`${start.line}:${start.column}-${end.line}:${end.column}`;
95101

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

281287
// nothing called on "expect()"
282288
'CallExpression:exit'(node: TSESTree.CallExpression) {
283-
if (
284-
isExpectCall(node) &&
285-
node.parent.type === AST_NODE_TYPES.ExpressionStatement
286-
) {
289+
if (isExpectCall(node) && isNoAssertionsParentNode(node.parent)) {
287290
context.report({ messageId: 'noAssertions', node });
288291
}
289292
},

0 commit comments

Comments
 (0)