Pattern: Missing test hook
Issue: -
Test setup and cleanup should be contained within Jest hooks (beforeAll, beforeEach, afterEach, afterAll) rather than being executed directly in the test file or describe block.
Example of incorrect code:
const setup = () => {
database.connect();
};
setup(); // Direct call outside hook
describe("tests", () => {
mockFunction(); // Direct mock setup
test("example", () => {
expect(database.isConnected()).toBe(true);
});
});
Example of correct code:
const setup = () => {
database.connect();
};
beforeAll(() => {
setup();
});
describe("tests", () => {
beforeEach(() => {
mockFunction();
});
test("example", () => {
expect(database.isConnected()).toBe(true);
});
});