Javascript: Mocha arrow-function and unnamed function errors.

Created on 21 Apr 2017  路  12Comments  路  Source: airbnb/javascript

Hello community I am receiving errors whenever I am using using describe(function(){}) in my code.
How can I get rid of these errors?
Here is my .eslintrc.yml.

extends: airbnb-base
plugins:
  - standard
  - promise
env:
    node: true
    mocha: true
rules:
    indent: ["error", 2]
    semi: ["error", "always", {omitLastInOneLineBlock: true}]
    no-eval: ["error"]
    no-trailing-spaces: ["error"]
    one-var-declaration-per-line: ["error", "initializations"]
    no-irregular-whitespace: ["error"]
    no-var: ["error"]
    no-empty: ["error"]
    linebreak-style: ["error", "unix"]
    eol-last: ["error", "always"]
    no-console: ["warn", {"allow": ["warn", "error", "info"]}]
    no-use-before-define: ["error", { "functions": false }]
    no-unused-vars: ["error", {"argsIgnorePattern":"^next"}]

Tried to add mocha-plugin like so:

plugins:
- mocha
rules:
mocha/no-arrow-function: "error"

but it still gives me errors.

Most helpful comment

For anyone stumbling here in 2019, I followed ljharb's advice and added the following .eslintrc config in my test directory

{
  "rules": {
    "prefer-arrow-callback": "off",
    "func-names": "off"
  }
}

All 12 comments

I'm not familiar with the mocha plugin; but our guide requires you either name your functions or use arrow functions, especially and including in mocha tests.

I've named my function, but it still outputs error: prefer-arrow-callback.

Can you provide the error output?

image
I would like to remove prefer-arrow-callback error as well unnamed function warning in mocha tests.

Can you also provide the exact code that's producing those errors?

Sure :)

describe('/articles', function() {
  describe('GET', function() {
    before(function(done) {
      app.initializeServer(done);
    });
    describe('#getOneById', function() {
      describe('article _id is a correct number', function() {
        it('response.statusCode should be 200', function(done) {
          request
            .get('/api/articles/1')
            .expect(200, done);
        });
        it('response.body should equal articleSchema', function(done) {
          request
            .get('/api/articles/1')
            .end((err, res) => {
              expect(validator.validate(res.body, articleSchema)).to.equal(true);
              expect(err).to.be.a('null');
              done();
            });
        });
      });
      describe('article _id is a string', function() {
        it('response.statusCode should be ...', function(done) {
          done();
        });
      });
      describe('article _id is a invalid number', function() {
        it('response.statusCode should be ...', function(done) {
          done();
        });
      });
    });
    describe('#getAll', function() {
      it('should respond with 10 articles', function(done) {
      request
        .get('/api/articles/')
        .expect(200)
        .end((err,res) => {
          expect(err).to.be.a('null');
          expect(validator.validate(res.body, articlesCollectionSchema)).to.equal(true);
          done();
        });
      });
    });
  });
});

ok so - none of those functions are named. function (done) { is an anonymous function - that either needs to be (done) => {, or function something(done) {.

As i said even if I name a function I am still receiving error with prefer-arrow-callback

Ah. why do you want them to not be arrows?

Because using arrows in mocha is discouraged.

Only if you need to use this for timeouts and the like; which this eslint rule will allow.

Using arrows in mocha is encouraged by this guide; mocha is wrong to discourage them.

You can certainly create a test/.eslintrc to disable rules just for tests, if you insist.

For anyone stumbling here in 2019, I followed ljharb's advice and added the following .eslintrc config in my test directory

{
  "rules": {
    "prefer-arrow-callback": "off",
    "func-names": "off"
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

golopot picture golopot  路  3Comments

ar
mbifulco picture mbifulco  路  3Comments

weihongyu12 picture weihongyu12  路  3Comments

progre picture progre  路  3Comments

felixsanz picture felixsanz  路  3Comments