Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(no-focused-tests): detect table format uage of .only.each (#489)
* refactor(no-focused-tests): use `isSupportedAccessor` utility function

* fix(no-focused-tests): detect table format usage of `.only.each`
  • Loading branch information
G-Rath committed Nov 30, 2019
1 parent 45c6b3b commit d03bcf4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
12 changes: 12 additions & 0 deletions src/rules/__tests__/no-focused-tests.test.ts
Expand Up @@ -34,6 +34,10 @@ ruleTester.run('no-focused-tests', rule, {
code: 'describe.only.each()',
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
},
{
code: 'describe.only.each`table`()',
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
},
{
code: 'describe["only"]()',
errors: [{ messageId: 'focusedTest', column: 10, line: 1 }],
Expand All @@ -46,6 +50,10 @@ ruleTester.run('no-focused-tests', rule, {
code: 'it.only.each()',
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
},
{
code: 'it.only.each`table`()',
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
},
{
code: 'it["only"]()',
errors: [{ messageId: 'focusedTest', column: 4, line: 1 }],
Expand All @@ -58,6 +66,10 @@ ruleTester.run('no-focused-tests', rule, {
code: 'test.only.each()',
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
},
{
code: 'test.only.each`table`()',
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
},
{
code: 'test["only"]()',
errors: [{ messageId: 'focusedTest', column: 6, line: 1 }],
Expand Down
31 changes: 12 additions & 19 deletions src/rules/no-focused-tests.ts
Expand Up @@ -2,7 +2,12 @@ import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { DescribeAlias, TestCaseName, createRule } from './utils';
import {
DescribeAlias,
TestCaseName,
createRule,
isSupportedAccessor,
} from './utils';

const testFunctions = new Set<string>([
DescribeAlias.describe,
Expand All @@ -17,14 +22,9 @@ const matchesTestFunction = (object: TSESTree.LeftHandSideExpression) =>
const isCallToFocusedTestFunction = (object: TSESTree.Identifier) =>
object.name.startsWith('f') && testFunctions.has(object.name.substring(1));

const isPropertyNamedOnly = (
property: TSESTree.Expression | TSESTree.Identifier,
) =>
('name' in property && property.name === 'only') ||
('value' in property && property.value === 'only');

const isCallToTestOnlyFunction = (callee: TSESTree.MemberExpression) =>
matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property);
matchesTestFunction(callee.object) &&
isSupportedAccessor(callee.property, 'only');

export default createRule({
name: __filename,
Expand All @@ -44,7 +44,10 @@ export default createRule({
defaultOptions: [],
create: context => ({
CallExpression(node) {
const { callee } = node;
const callee =
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
? node.callee.tag
: node.callee;

if (callee.type === AST_NODE_TYPES.MemberExpression) {
if (
Expand Down Expand Up @@ -78,16 +81,6 @@ export default createRule({
) {
context.report({ messageId: 'focusedTest', node: callee });
}

if (
callee.type === AST_NODE_TYPES.TaggedTemplateExpression &&
callee.tag.type === AST_NODE_TYPES.MemberExpression &&
callee.tag.object &&
callee.tag.object.type === AST_NODE_TYPES.Identifier &&
isCallToFocusedTestFunction(callee.tag.object)
) {
context.report({ messageId: 'focusedTest', node: callee });
}
},
}),
});

0 comments on commit d03bcf4

Please sign in to comment.