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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust no focused tests to support tables #489

Merged
merged 2 commits into from Nov 30, 2019
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
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 });
}
},
}),
});