Skip to content

Files

Latest commit

 

History

History
36 lines (29 loc) · 668 Bytes

no-hooks.md

File metadata and controls

36 lines (29 loc) · 668 Bytes

Pattern: Use of test hook

Issue: -

Description

Using Jest setup and teardown hooks (beforeAll, beforeEach, afterAll, afterEach) promotes shared state between tests, making them harder to understand and maintain in isolation.

Examples

Example of incorrect code:

describe("suite", () => {
  let data;
  beforeEach(() => {
    data = setupData();
  });
  afterEach(() => {
    data = null;
  });
  
  test("case", () => {
    expect(data.value).toBe(true);
  });
});

Example of correct code:

describe("suite", () => {
  test("case", () => {
    const data = setupData();
    expect(data.value).toBe(true);
  });
});