Skip to content

Support additional async matchers when detecting handled promises #1039

@puglyfe

Description

@puglyfe

What rule do you want to change?

await-async-queries

Does this change cause the rule to produce more or fewer warnings?

Fewer warnings

How will the change be implemented?

Expand the matching logic in hasClosestExpectResolvesRejects() to include expectAsync as well as the async matchers provided by jasmine. The implementation diff would look something like this (I'd probably also rename this function):

diff --git a/lib/node-utils/index.ts b/lib/node-utils/index.ts
index 4aa8a47..7a5a858 100644
--- a/lib/node-utils/index.ts
+++ b/lib/node-utils/index.ts
@@ -525,6 +525,12 @@ const matcherNamesHandlePromise = [
 	'rejects',
 	'toResolve',
 	'toReject',
+	'toBeRejected',
+	'toBeRejectedWith',
+	'toBeRejectedWithError',
+	'toBePending',
+	'toBeResolved',
+	'toBeResolvedTo',
 ];
 
 /**
@@ -539,7 +545,7 @@ export function hasClosestExpectResolvesRejects(node: TSESTree.Node): boolean {
 		ASTUtils.isIdentifier(node.callee) &&
 		node.parent &&
 		isMemberExpression(node.parent) &&
-		node.callee.name === 'expect'
+		['expect', 'expectAsync'].includes(node.callee.name)
 	) {
 		const expectMatcher = node.parent.property;
 		return (

Example code

expectAsync(
	screen.findByText('some text that should never appear')
).toBeRejected();

How does the current rule affect the code?

This code would fail the lint check and would need to be annotated with /* eslint-disable-next-line testing-library/await-async-utils */.

How will the new rule affect the code?

This code would be valid and would require no additional annotation.

Anything else?

I would understand the hesitancy to add additional framework-specific logic, but it's a fairly trivial addition that would reduce a number of false positives.

Do you want to submit a pull request to change the rule?

Yes

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions