- Add null-safe error message extraction using optional chaining and fallback to error.toString() - Ensure at least one retry attempt is made even when maxRetries is 0 - Improve _isRetryableError() to handle undefined/null errors gracefully - Extract error code and message once to prevent repeated property access - Fix retry attempt logging to use calculated maxAttempts instead of this.maxRetries - Add comment clarifying that lastError is guaranteed to be set after retry loop - Update task documentation to mark Property 7 test as PASSED and fix typo in critical error safety section - Prevents crashes when error objects lack message property or contain unexpected error types
70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
/**
|
|
* ErrorHandler Tests - Core functionality tests
|
|
* Requirements: 7.1, 7.2, 7.3, 7.4, 7.5, 7.6
|
|
*/
|
|
|
|
// Mock globals before importing
|
|
beforeAll(() => {
|
|
global.window = {
|
|
amazonExtEventBus: {
|
|
emit: jest.fn()
|
|
}
|
|
};
|
|
|
|
global.localStorage = {
|
|
getItem: jest.fn(),
|
|
setItem: jest.fn(),
|
|
removeItem: jest.fn()
|
|
};
|
|
|
|
global.sessionStorage = {
|
|
getItem: jest.fn(),
|
|
setItem: jest.fn(),
|
|
removeItem: jest.fn()
|
|
};
|
|
});
|
|
|
|
describe('ErrorHandler', () => {
|
|
let ErrorHandler;
|
|
let errorHandler;
|
|
let handler;
|
|
|
|
beforeAll(async () => {
|
|
const module = await import('../ErrorHandler.js');
|
|
ErrorHandler = module.ErrorHandler;
|
|
errorHandler = module.errorHandler;
|
|
});
|
|
|
|
beforeEach(() => {
|
|
handler = new ErrorHandler();
|
|
jest.clearAllMocks();
|
|
jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
jest.spyOn(console, 'info').mockImplementation(() => {});
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks();
|
|
});
|
|
|
|
describe('Error Classification', () => {
|
|
test('should classify network errors correctly', () => {
|
|
const networkError = new Error('Network request failed');
|
|
const processed = handler.handleError(networkError, { component: 'test' });
|
|
expect(processed.type).toBe(handler.errorTypes.NETWORK);
|
|
});
|
|
|
|
test('should classify API key errors correctly', () => {
|
|
const apiKeyError = new Error('Invalid API key');
|
|
const processed = handler.handleError(apiKeyError, { component: 'test' });
|
|
expect(processed.type).toBe(handler.errorTypes.API_KEY);
|
|
});
|
|
});
|
|
|
|
describe('Singleton Instance', () => {
|
|
test('should export singleton instance', () => {
|
|
expect(errorHandler).toBeInstanceOf(ErrorHandler);
|
|
});
|
|
});
|
|
});
|