|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { BaseError, AuthenticationError, APIError, ConfigurationError } from './errors.js'; |
| 3 | + |
| 4 | +describe('Error Classes', () => { |
| 5 | + describe('BaseError', () => { |
| 6 | + it('should create a BaseError with all properties', () => { |
| 7 | + const context = { userId: '123', action: 'test' }; |
| 8 | + const error = new BaseError('Test message', 'UNEXPECTED_ERROR', { context }); |
| 9 | + |
| 10 | + expect(error).toBeInstanceOf(Error); |
| 11 | + expect(error).toBeInstanceOf(BaseError); |
| 12 | + expect(error.message).toBe('Test message'); |
| 13 | + expect(error.code).toBe('UNEXPECTED_ERROR'); |
| 14 | + expect(error.context).toEqual(context); |
| 15 | + expect(error.name).toBe('BaseError'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should create a BaseError without context', () => { |
| 19 | + const error = new BaseError('Test message', 'UNEXPECTED_ERROR'); |
| 20 | + |
| 21 | + expect(error.message).toBe('Test message'); |
| 22 | + expect(error.code).toBe('UNEXPECTED_ERROR'); |
| 23 | + expect(error.context).toBeUndefined(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should maintain proper stack trace', () => { |
| 27 | + const error = new BaseError('Test message', 'UNEXPECTED_ERROR'); |
| 28 | + expect(error.stack).toBeDefined(); |
| 29 | + expect(error.stack).toContain('BaseError'); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('AuthenticationError', () => { |
| 34 | + it('should create an AuthenticationError with proper inheritance', () => { |
| 35 | + const error = new AuthenticationError('Invalid token'); |
| 36 | + |
| 37 | + expect(error).toBeInstanceOf(Error); |
| 38 | + expect(error).toBeInstanceOf(BaseError); |
| 39 | + expect(error).toBeInstanceOf(AuthenticationError); |
| 40 | + expect(error.message).toBe('Invalid token'); |
| 41 | + expect(error.code).toBe('AUTHENTICATION_ERROR'); |
| 42 | + expect(error.name).toBe('AuthenticationError'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should create an AuthenticationError with context', () => { |
| 46 | + const context = { tokenType: 'Bearer', expiry: '2024-01-01' }; |
| 47 | + const error = new AuthenticationError('Token expired', { context }); |
| 48 | + |
| 49 | + expect(error.message).toBe('Token expired'); |
| 50 | + expect(error.context).toEqual(context); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('APIError', () => { |
| 55 | + it('should create an APIError with status code', () => { |
| 56 | + const error = new APIError('Server error', { statusCode: 500 }); |
| 57 | + |
| 58 | + expect(error).toBeInstanceOf(Error); |
| 59 | + expect(error).toBeInstanceOf(BaseError); |
| 60 | + expect(error).toBeInstanceOf(APIError); |
| 61 | + expect(error.message).toBe('Server error'); |
| 62 | + expect(error.code).toBe('API_ERROR'); |
| 63 | + expect(error.statusCode).toBe(500); |
| 64 | + expect(error.name).toBe('APIError'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should create an APIError without status code', () => { |
| 68 | + const error = new APIError('Network error'); |
| 69 | + |
| 70 | + expect(error.message).toBe('Network error'); |
| 71 | + expect(error.statusCode).toBeUndefined(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should create an APIError with context and status code', () => { |
| 75 | + const context = { endpoint: '/api/test', method: 'GET' }; |
| 76 | + const error = new APIError('Bad request', { context, statusCode: 400 }); |
| 77 | + |
| 78 | + expect(error.statusCode).toBe(400); |
| 79 | + expect(error.context).toEqual(context); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('ConfigurationError', () => { |
| 84 | + it('should create a ConfigurationError with proper inheritance', () => { |
| 85 | + const error = new ConfigurationError('Missing API key'); |
| 86 | + |
| 87 | + expect(error).toBeInstanceOf(Error); |
| 88 | + expect(error).toBeInstanceOf(BaseError); |
| 89 | + expect(error).toBeInstanceOf(ConfigurationError); |
| 90 | + expect(error.message).toBe('Missing API key'); |
| 91 | + expect(error.code).toBe('CONFIGURATION_ERROR'); |
| 92 | + expect(error.name).toBe('ConfigurationError'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should create a ConfigurationError with context', () => { |
| 96 | + const context = { configFile: 'config.json', requiredFields: ['apiKey'] }; |
| 97 | + const error = new ConfigurationError('Invalid configuration', { context }); |
| 98 | + |
| 99 | + expect(error.message).toBe('Invalid configuration'); |
| 100 | + expect(error.context).toEqual(context); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('Error serialization', () => { |
| 105 | + it('should serialize errors properly', () => { |
| 106 | + const context = { test: 'value' }; |
| 107 | + const error = new AuthenticationError('Test error', { context }); |
| 108 | + |
| 109 | + // Test that the error can be converted to JSON (useful for logging) |
| 110 | + const serialized = JSON.stringify({ |
| 111 | + name: error.name, |
| 112 | + message: error.message, |
| 113 | + code: error.code, |
| 114 | + context: error.context, |
| 115 | + }); |
| 116 | + |
| 117 | + const parsed = JSON.parse(serialized) as { |
| 118 | + name: string; |
| 119 | + message: string; |
| 120 | + code: string; |
| 121 | + context: Record<string, unknown>; |
| 122 | + }; |
| 123 | + expect(parsed.name).toBe('AuthenticationError'); |
| 124 | + expect(parsed.message).toBe('Test error'); |
| 125 | + expect(parsed.code).toBe('AUTHENTICATION_ERROR'); |
| 126 | + expect(parsed.context).toEqual(context); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments