[Mocha] Execution order of before, beforeEach, after, afterEach
I often found myself thinking “Wait? What’s the execution order of before, beforeEach, after, afterEach in Mocha?”, so I wrote this article.
To write the conclusion first:
They are executed in this order.
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.