| describe('Error Handling Tests', () => { | |
| beforeEach(() => { | |
| jest.clearAllMocks(); | |
| }); | |
| afterEach(() => { | |
| // Restore all mocks after each test | |
| jest.restoreAllMocks(); | |
| }); | |
| it('handles network error correctly', async () => { | |
| const errorMessage = 'Network Error'; | |
| axios.get.mockRejectedValue(new Error(errorMessage)); // Simulate a network error | |
| await expect(fetchData('https://api.example.com/user/1')).rejects.toThrow(errorMessage); | |
| expect(axios.get).toHaveBeenCalledWith('https://api.example.com/user/1'); | |
| }); | |
| it('handles server error (500) correctly', async () => { | |
| const errorResponse = { | |
| response: { status: 500, data: { message: 'Internal Server Error' } } | |
| }; | |
| axios.get.mockRejectedValue(errorResponse); // Simulate a 500 error | |
| await expect(fetchData('https://api.example.com/user/1')).rejects.toThrow('Internal Server Error'); | |
| }); | |
| }); |