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 mocha/prefer-arrow-callback rule does not work as documented #333

Open
justinhelmer opened this issue Nov 28, 2022 · 0 comments
Open

Comments

@justinhelmer
Copy link

This is a great rule, but the documentation (which appears to just come from the original core rule) implies that arrow functions are preferred.

Instead, the mocha-aware rule discourages arrow functions. The reasoning totally resonates.

What I would have expected is, that the rule would behave as described by the documentation:

The following examples will not be flagged:

// arrow function callback
foo(a => a); // OK

... then, when used in combination with the allowUnboundThis rule, we could get the best of both worlds - arrow functions by default, but function expressions when this is used:

{ "allowUnboundThis": false } will flag the following examples:

foo(function() { this.a; });

foo(function() { (() => this); });

someArray.map(function(itm) { return this.doSomething(itm); }, someObject);

I tried disabling the no-mocha-arrows rule thinking that it might create a conflict, but still the --fix replaces my arrow functions with function expressions. I would expect (based on the documentation) that configuration like this:

module.exports = {
  env: {
    mocha: true,
  },
  extends: ['plugin:mocha/recommended'],
  rules: {
    'no-mocha-arrows': 'off',
    'prefer-arrow-callback': 'off',
    'mocha/prefer-arrow-callback': 'error',
  },
};

...would produce results that look like this:

describe('this is not used so it keeps the arrow fn', () => {
  beforeEach(function () {
    this.foo = true;
  });
  
  it('should keep the arrow fn', () => {
    expect(true).toBe(true);
  });
  
  it('should replace the arrow fn with a function expression', function () {
    expect(this.foo).toBe(true);
  });
});

Does this make sense?

Thanks for the great plugin. This is not a deal-breaker, just a bit more verbose than it needs to be since arrow functions can be preserved according to the core ESLint rule.

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

1 participant