lo1tuma / eslint-plugin-mocha

ESLint rules for mocha
MIT License
279 stars 61 forks source link

mocha/no-top-level-hooks with test generator function #331

Closed tomerghelber-tm closed 1 year ago

tomerghelber-tm commented 1 year ago

Hello,

We have in our code base a lot of functions that generate test suites.

Those functions catch the rule mocha/no-top-level-hooks and I am not sure it should. Why doesn't it work only if they are on the top level?

They also catch the rule of mocha/no-exports and mocha/no-identical-title. Is this their expected behavior?

Thank you for the work

lo1tuma commented 1 year ago

Hi 👋,

the no-top-level-hooks basically forbids to define hooks outside of a suite (e.g. describe()). Using hooks in a regular function means it could be potentially outside of a suite, because we don’t know where and when this function is called. How exactly does your function look like? Would it be possible to create the suite within the function? I would assume it should work like that:


function createSuite(title) {
  describe(title, function () {
    beforeEach(function () {
       // to stuff...
    });
    it('works', function () {
      // verify stuff...
    });
  });
}
tomerghelber-tm commented 1 year ago

I understand now, thank you for the explanation! I will try to fix the function with a suite.