|
| 1 | +import { describe, expect, it, mock } from 'bun:test' |
| 2 | +import { italic } from '@stacksjs/cli' |
| 3 | +import { ExitCode } from '@stacksjs/types' |
| 4 | +import { ErrorHandler, handleError } from '../src/handler' |
| 5 | +import { rescue } from '../src/utils' |
| 6 | + |
| 7 | +// Tests for ErrorHandler class |
| 8 | +describe('@stacksjs/error-handling', () => { |
| 9 | + describe('ErrorHandler', () => { |
| 10 | + it('should handle string errors', () => { |
| 11 | + const error = 'Test error' |
| 12 | + const handledError = ErrorHandler.handle(error) |
| 13 | + expect(handledError).toBeInstanceOf(Error) |
| 14 | + expect(handledError.message).toBe(error) |
| 15 | + }) |
| 16 | + |
| 17 | + it('should handle Error objects', () => { |
| 18 | + const error = new Error('Test error') |
| 19 | + const handledError = ErrorHandler.handle(error) |
| 20 | + expect(handledError).toBe(error) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should write error to console when not silent', () => { |
| 24 | + const consoleErrorMock = mock(console, 'error') |
| 25 | + const error = new Error('Test error') |
| 26 | + ErrorHandler.handle(error, { silent: false }) |
| 27 | + expect(consoleErrorMock).toHaveBeenCalledWith(error) |
| 28 | + consoleErrorMock.mockRestore(redisTest) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should write error to file', async () => { |
| 32 | + const error = new Error('Test error') |
| 33 | + await ErrorHandler.writeErrorToFile(error) |
| 34 | + // You can add more checks here to verify the file writing logic |
| 35 | + }) |
| 36 | + |
| 37 | + it('should handle specific command errors', () => { |
| 38 | + const consoleErrorMock = mock(console, 'error') |
| 39 | + const processExitMock = mock(process, 'exit') |
| 40 | + |
| 41 | + const error = `Failed to execute command: ${italic('bunx --bun biome check --fix')}` |
| 42 | + ErrorHandler.writeErrorToConsole(error) |
| 43 | + expect(consoleErrorMock).toHaveBeenCalledWith(error) |
| 44 | + expect(processExitMock).toHaveBeenCalledWith(ExitCode.FatalError) |
| 45 | + |
| 46 | + consoleErrorMock.mockRestore() |
| 47 | + processExitMock.mockRestore() |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + // Tests for utility functions |
| 52 | + describe('Utils', () => { |
| 53 | + it('should return the result of the function if no error occurs', () => { |
| 54 | + const result = rescue(() => 42, 0) |
| 55 | + expect(result).toBe(42) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should return the fallback value if an error occurs', () => { |
| 59 | + const result = rescue(() => { |
| 60 | + throw new Error('Test error') |
| 61 | + }, 0) |
| 62 | + expect(result).toBe(0) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + // Tests for handleError function |
| 67 | + describe('handleError', () => { |
| 68 | + it('should handle string errors', () => { |
| 69 | + const error = 'Test error' |
| 70 | + const handledError = handleError(error) |
| 71 | + expect(handledError).toBeInstanceOf(Error) |
| 72 | + expect(handledError.message).toBe(error) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should handle Error objects', () => { |
| 76 | + const error = new Error('Test error') |
| 77 | + const handledError = handleError(error) |
| 78 | + expect(handledError).toBe(error) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + // Tests for index.ts exports |
| 83 | + describe('ErrorHandling Index', () => { |
| 84 | + it('should export ErrorHandler', () => { |
| 85 | + expect(ErrorHandler).toBeDefined() |
| 86 | + }) |
| 87 | + |
| 88 | + it('should export handleError', () => { |
| 89 | + expect(handleError).toBeDefined() |
| 90 | + }) |
| 91 | + |
| 92 | + it('should export rescue', () => { |
| 93 | + expect(rescue).toBeDefined() |
| 94 | + }) |
| 95 | + }) |
| 96 | +}) |
0 commit comments