eslint-plugin-jest icon indicating copy to clipboard operation
eslint-plugin-jest copied to clipboard

[`require-hook`] Add option to allow class calls

Open marekdedic opened this issue 2 years ago • 2 comments
trafficstars

Hi, I have a lot of tests that are really similar, so I built a class that scaffolds all the tests and invoke it in my test file like so:

new NodeExtensionTester(new ExtensionToTest())
  .shouldMatchUnistNode({ type: "thematicBreak" })
  .shouldNotMatchUnistNode({ type: "horizontal_rule" })
  .shouldNotMatchUnistNode({ type: "other" })
  .runTests();

However, this triggers jest/require-hook. In that rule, there is the allowedFunctionCalls option. However, when I printed getNodeName(node) here, I got

NodeExtensionTester.shouldMatchUnistNode.shouldNotMatchUnistNode.shouldNotMatchUnistNode.runTests

... which is ugly and depends on all the methods called, so there's not really any point to using allowedFunctionCalls for this (instead of disable comments or disabling the rule altogether)

Could we add something like allowedClasses?

marekdedic avatar Jun 26 '23 17:06 marekdedic

Is there a reason why you're running this rule against your file if you have this custom extension? I'm not against adjusting the options to make the rule more flexible, but would like to understand more of your use-case first since I'm not sure why (or how) you'd want mix test cases with this (which is really when the result is needed - even if you're using things like jest.mock next to this, you can probably still safely disable require-hook)

G-Rath avatar Jun 30 '23 18:06 G-Rath

Yeah, I could disable this per-file in the config, that's true... I could also use disable comments. However, I expect to have more tests in more files that do not all use the class, so specifying which files to apply the rule to and which not would be a kind of pain...

Also you may want to use such a tester and then add some more tests manually in the same file, in which case you really have to use disable comments - I am not sure I'll be in such a scenario, but it's another option that comes to mind...

marekdedic avatar Jul 04 '23 18:07 marekdedic