Mocha afterEach hooks called in wrong order

Viewed 128

I have a set of tests written with Mocha. To do some basic setup and teardown for each test run, I use root hooks. Per the test file, there are additional beforeEach and afterEach hooks. The expected order would be:

root beforeEach
file beforeEach
test
file beforeEach
root beforeEach

But what I got is:

root beforeEach
file beforeEach
test
root beforeEach
file beforeEach

The simplified code looks like this:

// mocha-hooks.ts
export const mochaHooks = {
    beforeEach() {
        console.log('root beforeEach');
    },

    afterEach() {
        console.log('root afterEach');
    },
};
// sample.spec.ts
beforeEach(() => {
    console.log('file beforeEach');
});
afterEach(() => {
    console.log('file afterEach');
});

The project is written in Typescript and uses ts-mocha for the test execution. I already tried the legacy method with a file that contains a call to beforeEach and the new method of the exported mochaHooks object.

What is the point I'm missing?

0 Answers
Related