[Mocha] Execution order of before, beforeEach, after, afterEach
Tadashi Shigeoka · Sat, March 25, 2017
I often found myself thinking “Wait? What’s the execution order of before, beforeEach, after, afterEach in Mocha?”, so I wrote this article.
Execution order is before, beforeEach, afterEach, after
To write the conclusion first:
- before
- beforeEach
- test
- afterEach
- after
They are executed in this order.
Mocha Sample Code
Below is sample mocha code to check the execution order of before, beforeEach, afterEach, after.
describe('before test', function() {
before(function() {
console.log('before');
});
beforeEach(function() {
console.log('beforeEach');
});
after(function() {
console.log('after');
});
afterEach(function() {
console.log('afterEach');
});
it('test', function() {
console.log('test');
});
});
I want to write test code with a good understanding of what processes are called where.
That’s all from the Gemba.