javascript: Mocha tests: Missing function expression name. (func-names)

When writing Mocha tests, it is not possible to use fat arrow functions because we sometimes need to use the this keyword to change the test’s timeout for example.

describe('some description', function () {
  before('blabla', function () {
    this.timeout(10000);
    return somePromise();
  });
});

This results in the following eslint error:

Missing function expression name. (func-names)

I was wondering what was the guidelines for this case. Naming the functions is out of the question as it would add a lot of noise.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16

Commits related to this issue

Most upvoted comments

Using arrow functions in mocha tests is discouraged according to their official documentation.

I just don’t really see the point for mocha tests and it goes against the DRY principle. e.g.

    it('should return status 200', function shouldReturnStatus200() {
      expect(self.res.status).to.equal(200);
    });

Anyways, I added func-names: 0 to my tests/.eslintrc.

this is shared mutable state, and should always be avoided when possible. What would you say you use this for in your mocha tests? I can better suggest an alternative if I know your use case.

@merriam (1) is a flaw in mocha; it should provide another API that doesn’t depend on this. Re (2), the overarching value of ensuring all function expressions have names outweighs the slight redundancy when it’s used in a mocha test context.

Nothing is an animal that needs this, unless it was poorly designed in the first place to require it.

I’d recommend against using this in tests, if possible - doing that means you could use an arrow function.

As for the jest organization, that’s an inherent flaw with it - perhaps you could use two separate eslintrc/eslintignore pairs, one for implementation, one for tests? Otherwise perhaps that’s not the best way to organize tests.