Back to Mocha

Arrow Functions

docs-next/src/content/docs/features/arrow-functions.mdx

11.7.6924 B
Original Source

Passing arrow functions (aka "lambdas") to Mocha is discouraged. Lambdas lexically bind this and cannot access the Mocha context. For example, the following code will fail:

js
describe("my suite", () => {
  it("my test", () => {
    // should set the timeout of this test to 1000 ms; instead will fail
    this.timeout(1000);
    assert.ok(true);
  });
});

If you do not need to use Mocha's context, lambdas should work. Be aware that using lambdas will be more painful to refactor if the need eventually arises!

Alternatively, you can override certain context variables, such as test timeouts, by chain-calling methods of the created tests and/or hooks:

js
describe("my suite", () => {
  beforeEach(() => {}).timeout(1000);
  it("my test", () => {
    assert.ok(true);
  }).timeout(1000);
}).timeout(1000);