Error Handling
Error Handling
Missing Mock Verification
// Always verify mocks were called as expected
afterEach(() => {
// Fail if any mock was called unexpectedly
expect(unexpectedCallsDetected()).toBe(false);
});
// Use strict mocks
const strictMock = jest.fn().mockImplementation(() => {
throw new Error('Unexpected call');
});
Mock Leakage Between Tests
// Always reset mocks
beforeEach(() => {
jest.clearAllMocks(); // Clears call history
// or
jest.resetAllMocks(); // Also resets implementation
});