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

'no-sibling-hooks': false-positives inside functions #113

Open
nhardy opened this issue Oct 28, 2016 · 4 comments
Open

'no-sibling-hooks': false-positives inside functions #113

nhardy opened this issue Oct 28, 2016 · 4 comments

Comments

@nhardy
Copy link

nhardy commented Oct 28, 2016

I have a rough example below:

describe('a thing', () => {

  before(() => {
    // do some global setup
  });

  function runSomeTests() {
    before(() => { // <-- Incorrectly triggers 'mocha/no-sibling-hooks'
      // stuff
    });
    it('should test a different thing', () => {
      // test
    });
  }

  function runSomeOtherTests() {
    before(() => { // <-- Incorrectly triggers 'mocha/no-sibling-hooks'
      // stuff
    });
    it('should test a thing', () => {
      // test
    });
  }

  context('one thing', () => {
    before(() => {
      // more setup
    });
    context('nested thing', () => {
      runSomeTests();
    });
    context('other nested thing', () => {
      runSomeOtherTests();
    });
  });

  context('another thing', () => {
    before(() => {
      // different setup
    });
    context('nested thing', () => {
      runSomeTests();
    });
    context('other nested thing', () => {
      runSomeOtherTests();
    });
  });
});
@lo1tuma
Copy link
Owner

lo1tuma commented Nov 3, 2016

Thanks for the issue.

The problem with the example you provided is that it depends on where runSomeTests is called, so it could create a sibling hook if it would be called directly within the top-level describe function. I don't think there is a reliable way to detect such edge-cases with static code analysis. So the question is should we always skip hook functions that are not direct children of a describe function? Then the rule would't be able to detect all possible sibling hooks but it is probably less of a problem than having false positives.

@jfmengels
Copy link
Collaborator

This looks like a very complex setup for your tests, and maybe it makes sense in your case, but this seems like an edge case to me.

I would rather advocate to disable the rule on this file, unless you have a reasonable way to handle non-direct children.

I'd prefer handling this case correctly

describe('foo', function() {
  before(function() {});
  if (FOO) {
    before(function() {}); // No sibling hook
  }
});

(and also in switch cases, etc... you never know) than to allow @nhardy's example. Sometimes eslint-disable are fine. Feel free to disagree, and I don't mind if you go ahead and skip hook functions that are not direct children, but it'd be nice if we don't get a lot of false positives due to this change.

@ljharb
Copy link

ljharb commented Aug 6, 2020

I just got this when upgrading from v5 to v8 - i have a function setup() {} defined in a describe, and i'm calling it inside three neighboring its, and no-sibling-hooks warns on them.

I do not think this is an edge case; I think the rule is broken, because it's not using eslint's AST information to determine whether the hook functions are in fact globals (as opposed to file-local variables).

@lo1tuma
Copy link
Owner

lo1tuma commented Aug 6, 2020

@ljharb I think this is not the same issue as described here, but I fully agree with you that rules in this plugin should track where the identifiers are coming from (either globals or from an import/require). I hope to find some time in the next days to work on this.

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

No branches or pull requests

4 participants