Skip to content

Commit

Permalink
fix(valid-describe): support using each with modifiers (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Apr 10, 2021
1 parent ce76579 commit cbdbcef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
26 changes: 16 additions & 10 deletions src/rules/__tests__/valid-describe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ const ruleTester = new TSESLint.RuleTester({

ruleTester.run('valid-describe', rule, {
valid: [
'describe["each"]()()',
'describe["each"](() => {})()',
'describe["each"](() => {})("foo")',
'describe["each"]()(() => {})',
'describe["each"]("foo")(() => {})',
'describe.each([1, 2, 3])("%s", (a, b) => {});',
'describe("foo", function() {})',
'describe("foo", () => {})',
Expand Down Expand Up @@ -51,18 +46,21 @@ ruleTester.run('valid-describe', rule, {
}
`,
dedent`
describe.each\`
something | other
${1} | ${2} |
\`
("$something", ({ something, other }) => { });
describe.each\`
foo | foe
${1} | ${2}
\`('$something', ({ foo, foe }) => {});
`,
],
invalid: [
{
code: 'describe.each()()',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 1 }],
},
{
code: 'describe["each"]()()',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 1 }],
},
{
code: 'describe.each(() => {})()',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 1 }],
Expand All @@ -75,10 +73,18 @@ ruleTester.run('valid-describe', rule, {
code: 'describe.each()(() => {})',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 17 }],
},
{
code: 'describe["each"]()(() => {})',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 20 }],
},
{
code: 'describe.each("foo")(() => {})',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 22 }],
},
{
code: 'describe.only.each("foo")(() => {})',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 27 }],
},
{
code: 'describe(() => {})',
errors: [{ messageId: 'nameAndCallback', line: 1, column: 10 }],
Expand Down
15 changes: 0 additions & 15 deletions src/rules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,21 +763,6 @@ export const isDescribeCall = (
return false;
};

export const isDescribe = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<DescribeAlias> =>
(node.callee.type === AST_NODE_TYPES.Identifier &&
DescribeAlias.hasOwnProperty(node.callee.name)) ||
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.object.type === AST_NODE_TYPES.Identifier &&
DescribeAlias.hasOwnProperty(node.callee.object.name) &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
DescribeProperty.hasOwnProperty(node.callee.property.name)) ||
(node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression &&
node.callee.tag.type === AST_NODE_TYPES.MemberExpression &&
node.callee.tag.object.type === AST_NODE_TYPES.Identifier &&
DescribeAlias.hasOwnProperty(node.callee.tag.object.name));

/**
* Checks if the given node` is a call to `<describe|test|it>.each(...)()`.
* If `true`, the code must look like `<method>.each(...)()`.
Expand Down
4 changes: 2 additions & 2 deletions src/rules/valid-describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import {
createRule,
getJestFunctionArguments,
isDescribe,
isDescribeCall,
isEachCall,
isFunction,
} from './utils';
Expand Down Expand Up @@ -46,7 +46,7 @@ export default createRule({
return {
CallExpression(node) {
if (
!isDescribe(node) ||
!isDescribeCall(node) ||
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
) {
return;
Expand Down

0 comments on commit cbdbcef

Please sign in to comment.