|
| 1 | +import { stringifyValue } from '../utils'; |
| 2 | + |
| 3 | +describe('stringifyValue', () => { |
| 4 | + it('should JSON.stringify plain objects instead of producing [object Object]', () => { |
| 5 | + const obj = { errorId: 'abc-123', details: 'something broke' }; |
| 6 | + const result = stringifyValue(obj); |
| 7 | + |
| 8 | + expect(result).not.toContain('[object Object]'); |
| 9 | + expect(result).toContain('errorId'); |
| 10 | + expect(result).toContain('abc-123'); |
| 11 | + expect(result).toBe('{"errorId":"abc-123","details":"something broke"}'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should handle nested objects', () => { |
| 15 | + const result = stringifyValue({ outer: { inner: 'value' } }); |
| 16 | + |
| 17 | + expect(result).not.toContain('[object Object]'); |
| 18 | + expect(result).toContain('inner'); |
| 19 | + expect(result).toBe('{"outer":{"inner":"value"}}'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should handle null', () => { |
| 23 | + expect(stringifyValue(null)).toBe('null'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should handle undefined', () => { |
| 27 | + expect(stringifyValue(undefined)).toBe('(undefined)'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should use error.message for Error objects instead of JSON.stringify', () => { |
| 31 | + const err = new Error('something failed'); |
| 32 | + const result = stringifyValue(err); |
| 33 | + |
| 34 | + // JSON.stringify(err) would produce "{}" because Error properties are non-enumerable |
| 35 | + expect(result).not.toBe('{}'); |
| 36 | + expect(result).toContain('something failed'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should handle Error objects with no message', () => { |
| 40 | + const err = new Error(); |
| 41 | + const result = stringifyValue(err); |
| 42 | + |
| 43 | + // Should fall back to toString() which gives "Error" |
| 44 | + expect(result).toBeTruthy(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return empty string for empty objects', () => { |
| 48 | + expect(stringifyValue({})).toBe(''); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return empty string for empty arrays', () => { |
| 52 | + expect(stringifyValue([])).toBe(''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle non-empty arrays', () => { |
| 56 | + const result = stringifyValue([1, 2, 3]); |
| 57 | + |
| 58 | + expect(result).not.toContain('[object Object]'); |
| 59 | + expect(result).toBe('[1,2,3]'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle strings as-is', () => { |
| 63 | + expect(stringifyValue('hello')).toBe('hello'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle numbers', () => { |
| 67 | + expect(stringifyValue(42)).toBe('42'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle booleans', () => { |
| 71 | + expect(stringifyValue(true)).toBe('true'); |
| 72 | + expect(stringifyValue(false)).toBe('false'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should handle objects with circular references gracefully', () => { |
| 76 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 77 | + const obj: any = { a: 1 }; |
| 78 | + obj.self = obj; |
| 79 | + |
| 80 | + // Should not throw, falls back to toString() |
| 81 | + const result = stringifyValue(obj); |
| 82 | + expect(result).toBeDefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should produce correct output for the reported console.error scenario', () => { |
| 86 | + // Simulates: console.error('FatalErrorBoundary caught an error', { errorId, error, errorInfo }) |
| 87 | + // The hdxReport method joins stringifyValue results with spaces |
| 88 | + const args = [ |
| 89 | + 'FatalErrorBoundary caught an error', |
| 90 | + { |
| 91 | + errorId: 'err-001', |
| 92 | + error: 'TypeError: Cannot read properties of null', |
| 93 | + errorInfo: { componentStack: 'at App' }, |
| 94 | + }, |
| 95 | + ]; |
| 96 | + |
| 97 | + const message = args.map((x) => stringifyValue(x)).join(' '); |
| 98 | + |
| 99 | + expect(message).not.toContain('[object Object]'); |
| 100 | + expect(message).toContain('FatalErrorBoundary caught an error'); |
| 101 | + expect(message).toContain('errorId'); |
| 102 | + expect(message).toContain('err-001'); |
| 103 | + }); |
| 104 | +}); |
0 commit comments