Skip to content

The mocha/prefer-arrow-callback rule does not work as documented #333

Open
@justinhelmer

Description

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.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions