mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser

Home Page:https://mochajs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🚀 Feature: New interface to support arrow functions

alexreardon opened this issue · comments

Currently functions that use () => cannot take advantage of the mocha context: http://mochajs.org/#arrow-functions.

However, arrow function usage could be improved by passing the context as an argument:

it('should do something cool', (done, context) => {
  context.timeout = 5000;
  ...
});

The complicating factor is the done argument. The presence of which makes the test async. Perhaps if you want to use the context argument then you need to also call done when appropriate.

function () { this.timeout = 5000; } is less typing than (done, context) => { context.timeout = 5000; }

Forcing the function to be async to enable people to access the context seems like a pretty annoying API.

I still have no clue why people want to use arrow functions when it explicitly does the wrong thing in mochas context

Came here for this, but I've also found https://github.com/skozin/arrow-mocha

commented

Here is a version which can work with mocha's --require option:
https://www.npmjs.com/package/mocha-context

@alexreardon thanks for the suggestion, You can always do the following as well:

it('some case', () => {
  expect(1 + 2).to.equal(3);
}).timeout(1000);

Replacing the default bdd interface with this is a non-starter.

It's possible to create an interface which passes the context as the first parameter, but would likely require modifications to Mocha's core to support, due to hardcoded assumptions.

Would accept a PR that would allow the "async check" of an interface to be configurable. Once that's ready, then it should be fairly trivial to provide an alternate interface.

commented

allow the "async check" of an interface to be configurable.

What does it mean?

I think this feature could be implemented in 2 ways:

  1. Create a new interface e.g. bdd-arrow that would decorate the arrow function before sending fn to new Test.
  2. Add a new option e.g. mocha --context-argument that would change how runnable.fn is called.

Yeah, this would be a pretty big change to Mocha that is already solved by at least one userland integration mentioned in this thread. Per #5027 we're not looking to do big shakeups any time soon.