Skip to content

Commit

Permalink
fix(no-useless-await): Fix false positive with expect.poll and `res…
Browse files Browse the repository at this point in the history
…olves`/`rejects`

Fixes #323
  • Loading branch information
mskelton committed Oct 21, 2024
1 parent 6369932 commit 23663fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/rules/no-useless-await.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,9 @@ runRuleTester('no-useless-await', rule, {

'await expect(page.locator(".my-element")).toBeVisible()',
'await expect(page.locator(".my-element")).toHaveText("test")',

'await expect.poll(() => getSlowStorageValue(page, "key")).toBe("value")',
'await expect(doSomething()).resolves.toThrow("No element found")',
'await expect(doSomething()).rejects.toThrow("No element found")',
],
})
11 changes: 9 additions & 2 deletions src/rules/no-useless-await.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rule } from 'eslint'
import ESTree from 'estree'
import { getStringValue, isPageMethod } from '../utils/ast'
import { getStringValue, isIdentifier, isPageMethod } from '../utils/ast'
import { createRule } from '../utils/createRule'
import { parseFnCall } from '../utils/parseFnCall'

Expand Down Expand Up @@ -109,7 +109,14 @@ export default createRule({

// await expect(true).toBe(true)
const call = parseFnCall(context, node)
if (call?.type === 'expect' && expectMatchers.has(call.matcherName)) {
if (
call?.type === 'expect' &&
!call.modifiers.some((modifier) =>
isIdentifier(modifier, /^(resolves|rejects)$/),
) &&
!call.members.some((member) => isIdentifier(member, 'poll')) &&
expectMatchers.has(call.matcherName)
) {
return fix(node.parent)
}
},
Expand Down

0 comments on commit 23663fb

Please sign in to comment.