Jest’s nesting of describes isn’t logical, meaning hooks like beforeEach doesn’t take into account the “nesting” of describes.
describe("top level describe", () => {
beforeAll(() => console. log("top level", "before all"));
beforeEach(() => console. log("top level", "before each" ));
test("top level test 1", () => console. log("top level test 1"));
test("top level test 2", () => console. log("top level test 2"));
describe("nested describe", () => {
beforeAll(() => console. log("nested", "before all"));
beforeEach(() => console. log("nested", "before each"));
test ("nested test 1", () => console. log("nested test 1"));
test("nested test 2", () => console. log("nested test 2"));
});
});Output (not what one would expect)
top level before all
top level before each
top level test 1
top level before each
top level test 2
nested before all
top level before each // here
nested before each
nested test 1
top level before each // here
nested before each
nested test 2