Skip to content

Files

Latest commit

 

History

History
42 lines (31 loc) · 722 Bytes

prefer-hooks-on-top.md

File metadata and controls

42 lines (31 loc) · 722 Bytes

Pattern: Misplaced hook

Issue: -

Description

While hooks can be placed anywhere in a test file, they are always executed in a specific order. Placing hooks between test cases makes the code flow harder to understand and can lead to confusion about execution order.

Examples

Example of incorrect code:

describe("suite", () => {
  it("test one", () => {});
  
  beforeEach(() => {
    setup();
  });
  
  it("test two", () => {});
  
  beforeAll(() => {
    init();
  });
});

Example of correct code:

describe("suite", () => {
  beforeAll(() => {
    init();
  });
  
  beforeEach(() => {
    setup();
  });

  it("test one", () => {});
  it("test two", () => {});
});