|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { ToonDecodeError } from '../../toon/src/index' |
| 3 | +import { formatError } from '../src/format-error' |
| 4 | + |
| 5 | +describe('formatError', () => { |
| 6 | + it('renders a decode error with line and source as a header, source line, and caret', () => { |
| 7 | + const error = new ToonDecodeError( |
| 8 | + 'Tabs are not allowed in indentation in strict mode', |
| 9 | + { line: 2, source: '\tb: 1' }, |
| 10 | + ) |
| 11 | + |
| 12 | + const output = formatError(error, { isVerbose: false }) |
| 13 | + |
| 14 | + expect(output).toBe( |
| 15 | + 'Failed to decode TOON at line 2: Tabs are not allowed in indentation in strict mode\n' |
| 16 | + + '\n' |
| 17 | + + ' 2 | →b: 1\n' |
| 18 | + + ' ^', |
| 19 | + ) |
| 20 | + }) |
| 21 | + |
| 22 | + it('renders a decode error without source as a header only', () => { |
| 23 | + const error = new ToonDecodeError('Something went wrong', { line: 5 }) |
| 24 | + |
| 25 | + const output = formatError(error, { isVerbose: false }) |
| 26 | + |
| 27 | + expect(output).toBe('Failed to decode TOON at line 5: Something went wrong') |
| 28 | + }) |
| 29 | + |
| 30 | + it('appends the cause chain under verbose mode', () => { |
| 31 | + const cause = new SyntaxError('Unterminated string: missing closing quote') |
| 32 | + const error = new ToonDecodeError( |
| 33 | + 'Unterminated string: missing closing quote', |
| 34 | + { line: 2, source: 'greeting: "hello', cause }, |
| 35 | + ) |
| 36 | + |
| 37 | + const output = formatError(error, { isVerbose: true }) |
| 38 | + |
| 39 | + expect(output).toContain('Failed to decode TOON at line 2:') |
| 40 | + expect(output).toContain(' 2 | greeting: "hello') |
| 41 | + expect(output).toContain('Caused by: SyntaxError: Unterminated string: missing closing quote') |
| 42 | + }) |
| 43 | + |
| 44 | + it('appends the stack trace under verbose mode and omits it otherwise', () => { |
| 45 | + const error = new ToonDecodeError('Boom', { line: 1, source: 'x' }) |
| 46 | + error.stack = 'ToonDecodeError: Line 1: Boom\n at fakeFrame (file.ts:1:1)' |
| 47 | + |
| 48 | + const verbose = formatError(error, { isVerbose: true }) |
| 49 | + const quiet = formatError(error, { isVerbose: false }) |
| 50 | + |
| 51 | + expect(verbose).toContain('at fakeFrame (file.ts:1:1)') |
| 52 | + expect(quiet).not.toContain('at fakeFrame') |
| 53 | + }) |
| 54 | + |
| 55 | + it('renders a generic Error as its message only when not verbose', () => { |
| 56 | + const error = new Error('something went wrong') |
| 57 | + |
| 58 | + const output = formatError(error, { isVerbose: false }) |
| 59 | + |
| 60 | + expect(output).toBe('Error: something went wrong') |
| 61 | + }) |
| 62 | + |
| 63 | + it('places the caret under the first non-whitespace character of the source line', () => { |
| 64 | + const error = new ToonDecodeError( |
| 65 | + 'Indentation must be exact multiple of 2, but found 3 spaces', |
| 66 | + { line: 2, source: ' b: 1' }, |
| 67 | + ) |
| 68 | + |
| 69 | + const output = formatError(error, { isVerbose: false }) |
| 70 | + |
| 71 | + expect(output).toBe( |
| 72 | + 'Failed to decode TOON at line 2: Indentation must be exact multiple of 2, but found 3 spaces\n' |
| 73 | + + '\n' |
| 74 | + + ' 2 | b: 1\n' |
| 75 | + + ' ^', |
| 76 | + ) |
| 77 | + }) |
| 78 | +}) |
0 commit comments