Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't consider template tags in the middle of a possible jest function chain to be valid #1133

Merged
merged 1 commit into from May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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