Mocha: enhancement: new interface to support arrow functions

Created on 3 Jan 2017  路  7Comments  路  Source: mochajs/mocha

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.

feature

All 7 comments

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

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.

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.

@eight04 see #3399

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamhooper picture adamhooper  路  3Comments

smithamax picture smithamax  路  4Comments

EdvinOlofsson picture EdvinOlofsson  路  3Comments

CADBOT picture CADBOT  路  3Comments

delta62 picture delta62  路  3Comments