Skip to content

Commit

Permalink
Fix 463 (#464)
Browse files Browse the repository at this point in the history
* chore(utils): create `isDescribeEach` guard

* fix(valid-describe): don't check arguments of describe.each

Fixes #463
  • Loading branch information
G-Rath committed Oct 31, 2019
1 parent d620388 commit c1ddbb2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/rules/__tests__/valid-describe.test.ts
Expand Up @@ -16,6 +16,7 @@ ruleTester.run('valid-describe', rule, {
'describe["each"](() => {})("foo")',
'describe["each"]()(() => {})',
'describe["each"]("foo")(() => {})',
'describe.each([1, 2, 3])("%s", (a, b) => {});',
'describe("foo", function() {})',
'describe("foo", () => {})',
'describe(`foo`, () => {})',
Expand Down
33 changes: 29 additions & 4 deletions src/rules/utils.ts
Expand Up @@ -550,22 +550,32 @@ export enum TestCaseProperty {
}

export type JestFunctionName = DescribeAlias | TestCaseName | HookName;
export type JestPropertyName = DescribeProperty | TestCaseProperty;

export interface JestFunctionIdentifier<FunctionName extends JestFunctionName>
extends TSESTree.Identifier {
name: FunctionName;
}

export interface JestFunctionMemberExpression<
FunctionName extends JestFunctionName
> extends TSESTree.MemberExpression {
FunctionName extends JestFunctionName,
PropertyName extends JestPropertyName = JestPropertyName
> extends KnownMemberExpression<PropertyName> {
object: JestFunctionIdentifier<FunctionName>;
}

export interface JestFunctionMemberExpression<
FunctionName extends JestFunctionName,
PropertyName extends JestPropertyName = JestPropertyName
> extends KnownMemberExpression<PropertyName> {
object: JestFunctionIdentifier<FunctionName>;
}

export interface JestFunctionCallExpressionWithMemberExpressionCallee<
FunctionName extends JestFunctionName
FunctionName extends JestFunctionName,
PropertyName extends JestPropertyName = JestPropertyName
> extends TSESTree.CallExpression {
callee: JestFunctionMemberExpression<FunctionName>;
callee: JestFunctionMemberExpression<FunctionName, PropertyName>;
}

export interface JestFunctionCallExpressionWithIdentifierCallee<
Expand Down Expand Up @@ -651,6 +661,21 @@ export const isDescribe = (
);
};

/**
* Checks if the given `describe` is a call to `describe.each`.
*
* @param {JestFunctionCallExpression<DescribeAlias>} node
* @return {node is JestFunctionCallExpression<DescribeAlias, DescribeProperty.each>}
*/
export const isDescribeEach = (
node: JestFunctionCallExpression<DescribeAlias>,
): node is JestFunctionCallExpressionWithMemberExpressionCallee<
DescribeAlias,
DescribeProperty.each
> =>
node.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.callee.property, DescribeProperty.each);

/**
* Gets the arguments of the given `JestFunctionCallExpression`.
*
Expand Down
3 changes: 2 additions & 1 deletion src/rules/valid-describe.ts
Expand Up @@ -6,6 +6,7 @@ import {
createRule,
getJestFunctionArguments,
isDescribe,
isDescribeEach,
isFunction,
} from './utils';

Expand Down Expand Up @@ -85,7 +86,7 @@ export default createRule({
});
}

if (callback.params.length) {
if (!isDescribeEach(node) && callback.params.length) {
context.report({
messageId: 'unexpectedDescribeArgument',
loc: paramsLocation(callback.params),
Expand Down

0 comments on commit c1ddbb2

Please sign in to comment.