Pattern: Use of test hook
Issue: -
Using Jest setup and teardown hooks (beforeAll, beforeEach, afterAll, afterEach) promotes shared state between tests, making them harder to understand and maintain in isolation.
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);
});
});