1
0
Fork 0
tsgames/test/common/errors.test.ts

57 lines
2.2 KiB
TypeScript

import { describe, it, expect } from 'bun:test';
import { formatError, formatErrorMessage } from '@common/errors';
describe('errors', () => {
describe('formatErrorMessage', () => {
it('should extract message from Error objects', () => {
const error = new Error('Test error message');
expect(formatErrorMessage(error)).toBe('Test error message');
});
it('should handle objects with message property', () => {
const obj = { message: 'Custom error' };
expect(formatErrorMessage(obj)).toBe('Custom error');
});
it('should convert other values to string', () => {
expect(formatErrorMessage('string error')).toBe('string error');
expect(formatErrorMessage(123)).toBe('123');
expect(formatErrorMessage(null)).toBe('Unknown error');
expect(formatErrorMessage(undefined)).toBe('Unknown error');
});
it('should handle plain objects without message', () => {
expect(formatErrorMessage({ foo: 'bar' })).toBe('[object Object]');
});
});
describe('formatError', () => {
it('should format error with custom message', () => {
const error = new Error('Test error');
const result = formatError(error, 'Context');
expect(result).toContain('Context: Test error');
});
it('should format error without custom message', () => {
const error = new Error('Test error');
const result = formatError(error);
expect(result).toContain('Test error');
expect(result).toContain('Error: Test error');
});
it('should handle string errors', () => {
const result = formatError('error string', 'Context');
expect(result).toBe('Context: error string');
});
it('should handle null/undefined errors', () => {
expect(formatError(null, 'Context')).toBe('Context: Unknown error');
expect(formatError(undefined, 'Context')).toBe('Context: Unknown error');
});
it('should handle number errors', () => {
expect(formatError(42, 'Context')).toBe('Context: 42');
});
});
});