Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(valid-expect): warn on await expect() with no assertions (#496)
  • Loading branch information
motiz88 authored and G-Rath committed Dec 28, 2019
1 parent 6ffb998 commit 19798dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/rules/valid-expect.md
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions src/rules/__tests__/valid-expect.test.ts
Expand Up @@ -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' }],
},
],
});
11 changes: 7 additions & 4 deletions src/rules/valid-expect.ts
Expand Up @@ -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}`;

Expand Down Expand Up @@ -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 });
}
},
Expand Down

0 comments on commit 19798dd

Please sign in to comment.