Running Mocha setup before each suite rather than before each test

@ya_dimon 's solution is working, but If you want to wrap the callback function of it, and pass parameter to it as follow.

function dynamicTestCase(a) {
  return function(done){ console.log(a); } // a is undefined
}

describe("test", function(){
    before(function(){
        a = 8;
    });
    it('POST /verifications receiveCode', dynamicTestCase(a)); // a is undefined
})

Why a is undefined? Because it is executed before before in describe. In this case, @ya_dimon 's solution could not work, but you could make It done trickly as following.

function dynamicTestCase(a) {
  return function(done){ console.log(a()); } // a is delayed pass! a = 8, remember change a to a()
}

describe("test", function(){
    before(function(){
        a = 8;
    });
    it('POST /verifications receiveCode', dynamicTestCase(() => a)); // a is delayed pass!
})

Hope this help to figure out the execution sequence.


you can also do it in this more flexible way:

require('./setupStuff');

describe('Suite one', function(){
  loadBeforeAndAfter(); //<-- added
  it('S1 Test one', function(done){
    ...
  });
  it('S1 Test two', function(done){
    ...
  });
});
describe('Suite two', function(){
  loadBeforeAndAfter();//<-- added
  it('S2 Test one', function(done){
    ...
  });
});
describe('Suite three', function(){
  //use some other loader here, before/after, or nothing
  it('S3 Test one', function(done){
    ...
  });
});

function loadBeforeAndAfter() {
  before(function () {
    console.log("shared before");
  });
  after(function () {
    console.log("shared after");
  });
}

There's no call similar to beforeEach or before that does what you want. But it is not needed because you can do it this way:

function makeSuite(name, tests) {
    describe(name, function () {
        before(function () {
            console.log("shared before");
        });
        tests();
        after(function () {
            console.log("shared after");
        });
    });
}

makeSuite('Suite one', function(){
  it('S1 Test one', function(done){
      done();
  });
  it('S1 Test two', function(done){
      done();
  });
});

makeSuite('Suite two', function(){
  it('S2 Test one', function(done){
    done();
  });
});