Make Mocha wait before running next test

In my case, I was coding a RESTful API in NodeJS that was manipulating some files locally. As I was launching the tests, the API was receiving multiple requests and it makes my API manipulate, concurrently, these files in the machine, which led me to a problem.

So, I needed to put some time (1 sec was enough) between these API calls. For me, the solution was the following one:

beforeEach( async () => {
   await new Promise(resolve => setTimeout(resolve, 1000));
   console.log("----------------------");
});

Now, before each it() test, the previous function is run, and I have a sleep of 1 second between API calls.


setTimeout definitely could help, but there may be a "cleaner" way to do it.

The documentation here actually says to use this.timeout(delay) to avoid timeout errors while testing async code, so be careful.

var global;

it('should give some info', function(done) {
  run.someMethod(param, function(err, result) {
    global = result.global
  done();
  });
});

it('should give more info', function(done) {
    this.timeout(30000);

    setTimeout(function () {
      run.anotherMethod(global, function(err, result) {
        expect(result).to.be.an('object');
        done();
      });
    }, 30000);
 });

While this.timeout() will extend the timeout of a single test, it's not the answer to your question. this.timeout() sets the timeout of your current test.

But don't worry, you should be fine anyway. Tests are not running in parallel, they're done in series, so you should not have a problem with your global approach.