Skip to content

The utility function "isTestCase" works wrong with some situations. #589

Closed
@374632897

Description

@374632897

The relative PR is #307 .
See the test case like below(referenced from no-export.test.ts):

{
  valid: [
    'describe("a test", () => { expect(1).toBe(1); })',
    'window.location = "valid"',
    'module.somethingElse = "foo";',
    'export const myThing = "valid"',
    'export default function () {}',
    'module.exports = function(){}',
    'module.exports.myThing = "valid";',
    'export const test = () => true; test("Hello");'
  ],
}

The last item of valid array declares a function named "test" and exports it. In this case, the utility function "isTestCase" makes a wrong check:

export const isTestCase = (
  node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<TestCaseName> =>
  (node.callee.type === AST_NODE_TYPES.Identifier &&
    TestCaseName.hasOwnProperty(node.callee.name)) ||
  (node.callee.type === AST_NODE_TYPES.MemberExpression &&
    node.callee.property.type === AST_NODE_TYPES.Identifier &&
    TestCaseProperty.hasOwnProperty(node.callee.property.name) &&
    ((node.callee.object.type === AST_NODE_TYPES.Identifier &&
      TestCaseName.hasOwnProperty(node.callee.object.name)) ||
      (node.callee.object.type === AST_NODE_TYPES.MemberExpression &&
        node.callee.object.object.type === AST_NODE_TYPES.Identifier &&
        TestCaseName.hasOwnProperty(node.callee.object.object.name))));

Once a file has a function named "test" declared and been called in the file, the file will be marked as a test file and this makes it work wrong with the rule "jest/no-export".

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions