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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
374632897 opened this issue May 20, 2020 · 1 comment
Closed
Labels

Comments

@374632897
Copy link

374632897 commented May 20, 2020

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

@374632897 374632897 changed the title The rule "Do not export from a test file (jest/no-export)" works wrong with some situations. The utility function "isTestCase" works wrong with some situations. May 20, 2020
@G-Rath
Copy link
Collaborator

G-Rath commented May 20, 2020

This behaviour is correct: any file that is linted by eslint-plugin-jest is assumed to be a test file, as that is the kind of files this plugin is designed to lint.

We do have plans to improve our checking for the jest globals, which would most likely result in this particular case no longer being flagged, but regardless you should use overrides property in your eslint config to apply the rules from this plugin to just your test files :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants