jest: [Bug]: jest-circus doesn't respect beforeAll / beforeEach / afterEach / afterAll order
Version
27.5.1
Steps to reproduce
const triggers: string[] = [];
describe('mock-instance-reset', () => {
  describe('test', () => {
    beforeAll(() => triggers.push('beforeAll:1'));
    afterAll(() => triggers.push('afterAll:1'));
    beforeAll(() => triggers.push('beforeAll:2'));
    afterAll(() => triggers.push('afterAll:2'));
    beforeEach(() => triggers.push('beforeEach:1'));
    beforeEach(() => triggers.push('beforeEach:2'));
    afterEach(() => triggers.push('afterEach:1'));
    afterEach(() => triggers.push('afterEach:2'));
    it('triggers test #1', () => {
      expect(1).toEqual(1);
    });
    it('triggers test #2', () => {
      expect(2).toEqual(2);
    });
    describe('nested', () => {
      beforeAll(() => triggers.push('beforeAll:3'));
      afterAll(() => triggers.push('afterAll:3'));
      beforeEach(() => triggers.push('beforeEach:3'));
      afterEach(() => triggers.push('afterEach:3'));
      it('triggers test #3', () => {
        expect(3).toEqual(3);
      });
    });
  });
  it('has expected order', () => {
    // first before is called the first
    // first after is called the last
    expect(triggers).toEqual([
      'beforeAll:1',
      'beforeAll:2',
      'beforeEach:1',
      'beforeEach:2',
      'afterEach:2',
      'afterEach:1',
      'beforeEach:1',
      'beforeEach:2',
      'afterEach:2',
      'afterEach:1',
      'beforeAll:3',
      'beforeEach:1',
      'beforeEach:2',
      'beforeEach:3',
      'afterEach:3',
      'afterEach:2',
      'afterEach:1',
      'afterAll:3',
      'afterAll:2',
      'afterAll:1',
    ]);
  });
});
Expected behavior
Based on documentation, https://jestjs.io/docs/setup-teardown, before hooks are FIFO, after hooks are LIFO.
it works like that in jest-jasmine2, whereas jest-circus doesn’t respect the order.
Actual behavior
jest-circus calls after hooks as FIFO.
        "beforeAll:1",
        "beforeAll:2",
        "beforeEach:1",
        "beforeEach:2",
    -   "afterEach:2", // expected before #1
        "afterEach:1",
    +   "afterEach:2", // called after #1
        "beforeEach:1",
        "beforeEach:2",
    +   "afterEach:1",
        "afterEach:2",
    -   "afterEach:1",
        "beforeAll:3",
        "beforeEach:1",
        "beforeEach:2",
        "beforeEach:3",
        "afterEach:3",
    +   "afterEach:1",
        "afterEach:2",
    -   "afterEach:1",
        "afterAll:3",
    -   "afterAll:2",
        "afterAll:1",
    +   "afterAll:2",
Additional context
No response
Environment
System:
    OS: macOS 12.3.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Binaries:
    Node: 16.13.2 - /opt/local/bin/node
    Yarn: 1.22.17 - /opt/local/bin/yarn
    npm: 8.7.0 - /Volumes/MGS/Projects/ng-mocks/node_modules/.bin/npm
  npmPackages:
    jest: 27.5.1 => 27.5.1
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 5
- Comments: 31 (3 by maintainers)
Commits related to this issue
- jest-circus: reverse order of afterEach, afterAll hooks fixes https://github.com/facebook/jest/issues/12678 — committed to justjake/jest by justjake 2 years ago
Bump