Skip to content

Commit

Permalink
fix: Check class methods in expect-expect (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton authored Oct 22, 2023
1 parent ecc406a commit e11113e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/rules/expect-expect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rule } from 'eslint';
import * as ESTree from 'estree';
import { isExpectCall, isIdentifier, isTest } from '../utils/ast';
import { dig, isExpectCall, isTest } from '../utils/ast';
import { getAdditionalAssertFunctionNames } from '../utils/misc';

function isAssertionCall(
Expand All @@ -9,9 +9,7 @@ function isAssertionCall(
) {
return (
isExpectCall(node) ||
additionalAssertFunctionNames.find((name) =>
isIdentifier(node.callee, name),
)
additionalAssertFunctionNames.find((name) => dig(node.callee, name))
);
}

Expand All @@ -38,7 +36,7 @@ export default {
if (isTest(node, ['fixme', 'only', 'skip'])) {
unchecked.push(node);
} else if (isAssertionCall(node, additionalAssertFunctionNames)) {
checkExpressions(context.getAncestors());
checkExpressions(context.sourceCode.getAncestors(node));
}
},
'Program:exit'() {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function getMatchers(
* Digs through a series of MemberExpressions and CallExpressions to find an
* Identifier with the given name.
*/
function dig(node: ESTree.Node, identifier: string | RegExp): boolean {
export function dig(node: ESTree.Node, identifier: string | RegExp): boolean {
return node.type === 'MemberExpression'
? dig(node.property, identifier)
: node.type === 'CallExpression'
Expand Down
15 changes: 14 additions & 1 deletion test/spec/expect-expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,20 @@ runRuleTester('expect-expect', rule, {
await assertCustomCondition(page)
})
`,
name: 'Global settings only',
name: 'Custom assert function',
settings: {
playwright: {
additionalAssertFunctionNames: ['assertCustomCondition'],
},
},
},
{
code: dedent`
test('should fail', async ({ myPage, page }) => {
await myPage.assertCustomCondition(page)
})
`,
name: 'Custom assert class method',
settings: {
playwright: {
additionalAssertFunctionNames: ['assertCustomCondition'],
Expand Down

0 comments on commit e11113e

Please sign in to comment.