meteor / guide

:book: Articles about Meteor best practices

Home Page:http://guide.meteor.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mocha examples should not use arrow functions

awwx opened this issue · comments

The Meteor Guide Mocha examples shouldn't use arrow functions, says the Mocha documentation:

Passing arrow functions to Mocha is discouraged. Their lexical binding of the this value makes them unable to access the Mocha context, and statements like this.timeout(1000); will not work inside an arrow function.

Oh interesting, I didn't know mocha used this. Sounds like a pretty legitimate thing to change, and we should probably do it in the Todos app as well: https://github.com/meteor/todos

Kind of a bummer, since it makes all the tests a lot more verbose, but it is the way it is!

@tmeasday something to remember.

Would you mind submitting a PR?

Yup, I ran into this today when I needed to call this.slow()

Would you mind submitting a PR?

Sadly I don't have enough time in my schedule to submit a PR for the Meteor Guide.

You might like to consider adding a "pull-requests-wanted" label to the issue, and then in the README encourage people who'd like to contribute to check those issues. (See for example https://github.com/meteor/meteor/labels/pull-requests-encouraged ... notice how you can give someone a URL which displays all the issues with that label).

Great idea! The guide is a great first place for people to contribute, we should definitely encourage that.

Hi guys - I can submit a quick PR for this. Would you prefer to see anonymous functions or named functions used in the Mocha examples? In either case we'll need to adjust the eslint rules a bit - for example if you want anonymous:

/* eslint-env mocha */
/* eslint-disable func-names, prefer-arrow-callback */

describe('something', function () {
  it('something', function () {
    // blah
  });
});

or if you want named:

/* eslint-env mocha */
/* eslint-disable prefer-arrow-callback */

describe('something', function describe() {
  it('something', function it() {
    // blah
  });
});

Let me know - thanks!

Damn that's a bit unfortunate. I don't have a preference personally though.

@hwillson given that the tests are already described with the first argument to describe and it, it would be needlessly verbose to name the function. (In support note how in the Mocha docs all the functions are anonymous).

As an aside, it's common to need to use function instead of =>. For example, the function you pass to Meteor.publish is called with this set to the publish handler object, which wouldn't work if you used an arrow function. A lint rule that said "always use => in preference to function" wouldn't work for a lot of things.

As a personal note, I'd be pretty happy if this simply stopped existing.

@awwx Yes, I agree with using anonymous functions in this case (I just wanted to check before submitting the PR). With regards to having a lint rule that recommends using arrow functions (where relevant), that's what the Airbnb rules do (that MDG is using). So to use anonymous functions with tests we'll need to include:

/* eslint-disable func-names, prefer-arrow-callback */

Not a huge deal, just something extra to consider.

PR coming shortly. I'll aim to fire a similar PR in for the todos app as well.

Just to add - there is some discussion about leveraging arrow functions in Mocha 3.

Thanks for the PR @hwillson! Going to close this.