Mocha nested before after function call order

eye-catchJavaScript/TypeScript
Sponsored links

I use mocha for writing unit tests. My test sometimes fails because of misunderstanding the order of nested before, beforeEach, after, and afterEach functions. To write tests correctly, we need to know the order.

I wrote this simple test code for a better grasp.
The version of mocha is 9.1.4.

import "mocha";
import { expect } from "chai";

describe("TOP", () => {
    before(() => {
        console.log("- TOP before");
    });
    beforeEach(() => {
        console.log("= TOP beforeEach");
    });

    after(() => {
        console.log("- TOP after");
    });
    afterEach(() => {
        console.log("= TOP afterEach");
    });

    it("top level test case", () => {
        console.log(" --- top level test done");
    });

    describe("Nested", () => {
        before(() => {
            console.log("  - Nested before");
        });

        beforeEach(() => {
            console.log("  = Nested beforeEach");
        });

        after(() => {
            console.log("  - Nested after");
        });
        afterEach(() => {
            console.log("  = Nested afterEach");
        });

        it("nested test case", () => {
            console.log(" --- nested test done");
        });

        describe("Nested-2", () => {
            before(() => {
                console.log("    - Nested-2 before");
            });

            beforeEach(() => {
                console.log("    = Nested-2 beforeEach");
            });

            after(() => {
                console.log("    - Nested-2 after");
            });
            afterEach(() => {
                console.log("    = Nested-2 afterEach");
            });
            it("nested-2 test case", () => {
                console.log("    --- nested-2 test done");
            });
        });
    });
});

The result is as follows.

  TOP
- TOP before
= TOP beforeEach
 --- top level test done
    ✔ top level test case
= TOP afterEach
    Nested
  - Nested before
= TOP beforeEach
  = Nested beforeEach
 --- nested test done
      ✔ nested test case
  = Nested afterEach
= TOP afterEach
      Nested-2
    - Nested-2 before
= TOP beforeEach
  = Nested beforeEach
    = Nested-2 beforeEach
    --- nested-2 test done
        ✔ nested-2 test case
    = Nested-2 afterEach
  = Nested afterEach
= TOP afterEach
    - Nested-2 after
  - Nested after
- TOP after

Let’s check one by one.

The top-level test looks as expected. after function is not called here because other tests have not been done yet.

  TOP
- TOP before
= TOP beforeEach
 --- top level test done
    ✔ top level test case
= TOP afterEach

The next is one level nested test. Top-level before is not called here because it has already been called.

    Nested
  - Nested before
= TOP beforeEach
  = Nested beforeEach
 --- nested test done
      ✔ nested test case
  = Nested afterEach
= TOP afterEach

Even if the nest is deep, the order is always the same.

      Nested-2
    - Nested-2 before
= TOP beforeEach
  = Nested beforeEach
    = Nested-2 beforeEach
    --- nested-2 test done
        ✔ nested-2 test case
    = Nested-2 afterEach
  = Nested afterEach
= TOP afterEach
    - Nested-2 after
  - Nested after
- TOP after

Comments

Copied title and URL