How to set timeout on before hook in mocha?

mocha --timeout
mocha before
mocha async/await timeout
nodejs mocha set timeout
mocha assert
mocha before async
mocha describe timeout
mocha-sidebar timeout

I want to set timeout value on before hook in mocha test cases. I know I can do that by adding -t 10000 on the command line of mocha but this will change every test cases timeout value. I want to find a way to change the timeout programmatically below is my code:

describe('test  ', () => {

  before((done) => {
        this.timeout(10000);
         ...

it will complain about the line this.timeout(1000) that timeout is not defined. How to set the timeout on before hook.

You need to set a timeout in your describe block rather than in the hook if you want it to affect all the tests in the describe. However, you need to use a "regular" function as the callback to describe rather than an arrow function:

describe('test', function () {
  this.timeout(10000);

  before(...);

  it(...);
});

In all places where you want to use this in a callback you pass to Mocha you cannot use an arrow function. You must use a "regular" function which has its own this value that can be set by Mocha. If you use an arrow function, the value of this won't be what Mocha wants it to be and your code will fail.

You could set a different timeout for your before hook but there are two things to consider:

  1. Here too you'd need to use a "regular" function rather than an arrow function so:

    before(function (done) { 
      this.timeout(10000);
    
  2. This would set a timeout only for the before hook and would not affect your tests.

this.timeout in before/beforeEach throws error · Issue #300 · mochajs , For async tests and hooks, ensure "done()" is called; if returning a bin/www", "​test": "mocha --timeout 10000" <= increase this from 1000 to  The -t, --timeout <ms> option allows you to set the timeout for your test cases. Mocha uses a default of 2 seconds. You can set the timeout by specifying the number of milliseconds or a value with an s suffix to specify the time in seconds. mocha -t 3000. is equivalent to: mocha --timeout 3s

You can also call timeout() on the return value from describe, like this:

describe('test', () => {
  before(...);
  it(...);
}).timeout(10000);

With this approach you can use arrow functions, because you're no longer relying on this.

timeout of 2000ms exceeded. Ensure the done() callback is being , Tests can appear before, after, or interspersed with your hooks. setTimeout(​function() { // do some setup describe('my suite', function() { // . Trying to specify the timeout for a before/beforeEach doesn't currently work using this.timeout()

as instructed at https://mochajs.org/#hook-level, you have to use a regular function call to set the timeout.

before(function(done) {
  this.timeout(3000); // A very long environment setup.
  setTimeout(done, 2500);
});

if you insist on use arrow or async function in the hook. You may do it this way:

before(function (done) {
  this.timeout(3000);
  (async () => {
    await initilizeWithPromise();
  })().then(done);
});

It is quite helpful and nice-looking if you got multiple async calls to be resolved in the hook.

updated: fuction def works well with async too. So this hook can be upgraded to

before(async function () {
  this.timeout(3000);
  await initilizeWithPromise();      
});

So it provides benefits from both this and await.

By the way, mocha works pretty fine with promises nowadays. If timeout is not a concern. Just do this:

before(async () => {
  await initilizeWithPromise();
});

Change default timeout for mocha, This timeout setting will be inherited by all nested suites and test-cases You can also apply hook-level timeouts to a test-case as shown in the  You have to set the Mocha timeout to at least 20 seconds (depends how long your tests are inside of the it function) christian-bromann closed this Aug 9, 2013 This comment has been minimized.

Calling this.timeout(milliseconds); in the before hook is correct. Anyway, you need to use a regular function for the hook (function (done) ...) rather than an arrow function (done => ...).

before(
    function(done) {
        this.timeout(10000);
        ...
    }
);

And the reason is that arrow functions have no this binding.

How to set execution order of mocha test cases in multiple files , I'm using Jest as a testing library and inside its setup hook (which is executed before all my tests), I spawn a child process which launches a testing server on a​  mocha --version 1.20.1 mocha --timeout 100 something 1) should take less than 500 2) should take less than 500 too 0 passing (215ms) 2 failing 1) something should take less than 500: Error: timeout of 100ms exceeded 2) something should take less than 500 too: Error: timeout of 100ms exceeded

Mocha, describe('my suite', () => { it('my test', () => { // should set the timeout of this test to With its default “BDD”-style interface, Mocha provides the hooks before()  For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. I tried everything that I could find online but nothing worked. Thank you for your help!

mocha - Timeouts and diffs, If the before hooks fail it makes no sense to run the tests. So maybe think about Is there an easy way to set the title of the root suite from within a mocha test? simple, flexible, fun. Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

Mocha before all hook timeout exceeded - node.js - html, Hooks. Mocha provides the hooks before() , after() , beforeEach() , and afterEach​() , which can be used to set up preconditions and clean up after your tests. setTimeout(function() { // do some setup describe('my suite',  And we'd certainly like to encourage people to do setup and cleanup on a more per-test basis to begin with; but -- if you want my ballpark estimate of the idea -- I expect the most bang for our buck we'd get out of fitting the concept of per-test setup/cleanup into Mocha's hook system would be the aforementioned "setup in the test and register

Comments
  • Note that setting the timeout in before, e.g. before(done => {this.timeout(10000); setTimeout(()=>{done()}, 2500)});, does not work. You must set the timeout before before.
  • @ericP The example you show does not change the timeout of the before hook because you are using an arrow function. So this.timeout() executes on the wrong this. If you change the function you pass to before to a regular function, then this.timeout() will operate on the correct this and the timeout will apply to your before hook. That was the first "thing to consider" I listed at the end of my answer.
  • The arrow function is explicitly so i can access the this of the describe function. Give it a try with and without arrow functions.
  • @ericP I did try it before I responded the first time. What I wrote is correct: if you just change your arrow function to a regular function in the code you show, the code will work. You decided to use code which my answer says you should not use, and then it does not work. Well...