Skip to content

Commit

Permalink
fix: don't consider template tags in the middle of a possible jest fu…
Browse files Browse the repository at this point in the history
…nction chain to be valid (#1133)
  • Loading branch information
G-Rath committed May 29, 2022
1 parent 44de2ad commit 430de17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/rules/utils/__tests__/parseJestFnCall.test.ts
Expand Up @@ -118,6 +118,9 @@ ruleTester.run('nonexistent methods', rule, {
'"something".describe()',
'[].describe()',
'new describe().only()',
'``.test()',
'test.only``()',
'test``.only()',
],
invalid: [],
});
Expand Down
15 changes: 11 additions & 4 deletions src/rules/utils/parseJestFnCall.ts
Expand Up @@ -160,21 +160,21 @@ export const parseJestFnCall = (
return null;
}

// ensure that the only call expression in the chain is at the end
// check that every link in the chain except the last is a member expression
if (
chain
.slice(0, chain.length - 1)
.some(nod => nod.parent?.type === AST_NODE_TYPES.CallExpression)
.some(nod => nod.parent?.type !== AST_NODE_TYPES.MemberExpression)
) {
return null;
}

const [first, ...rest] = chain;

const lastNode = chain[chain.length - 1];
const lastLink = getAccessorValue(chain[chain.length - 1]);

// if we're an `each()`, ensure we're the outer CallExpression (i.e `.each()()`)
if (isSupportedAccessor(lastNode, 'each')) {
if (lastLink === 'each') {
if (
node.callee.type !== AST_NODE_TYPES.CallExpression &&
node.callee.type !== AST_NODE_TYPES.TaggedTemplateExpression
Expand All @@ -183,6 +183,13 @@ export const parseJestFnCall = (
}
}

if (
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression &&
lastLink !== 'each'
) {
return null;
}

const resolved = resolveToJestFn(scope, getAccessorValue(first));

// we're not a jest function
Expand Down

0 comments on commit 430de17

Please sign in to comment.