Dhiraj Gagrai

Mocha hooks execution order


describe('mocha before hooks', function () {
    before(() => console.log('*** top-level before()'));
    beforeEach(() => console.log('*** top-level beforeEach()'));
    describe('nesting', function () {
        before(() => console.log('*** nested before()'));
        beforeEach(() => console.log('*** nested beforeEach()'));
        it('is a nested spec', () => true);
    });
});

Output:

//   mocha before hooks
// *** top-level before()
//     nesting
// *** nested before()
// *** top-level beforeEach()
// *** nested beforeEach()
//       ✓ is a nested spec
//
//
//   1 passing (8ms)

Referenced from GitHub Gist by harto