This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

vit / test / error-format.test.js
2.5 kB 89 lines
1// SPDX-License-Identifier: MIT 2// Copyright (c) 2026 sol pbc 3 4import { describe, test, expect } from 'bun:test'; 5import { collectCauses, formatError } from '../src/lib/error-format.js'; 6 7describe('error-format', () => { 8 test('formats a flat Error', () => { 9 const err = new Error('top level'); 10 11 expect(collectCauses(err)).toEqual([]); 12 expect(formatError(err)).toBe('top level'); 13 }); 14 15 test('formats nested causes', () => { 16 const root = new Error('top level'); 17 const middle = new Error('middle'); 18 const leaf = new Error('leaf'); 19 root.cause = middle; 20 middle.cause = leaf; 21 22 expect(collectCauses(root)).toEqual(['middle', 'leaf']); 23 expect(formatError(root)).toBe([ 24 'top level', 25 ' caused by: middle', 26 ' caused by: leaf', 27 ].join('\n')); 28 }); 29 30 test('formats a non-Error throwable as a flat value', () => { 31 expect(collectCauses('boom')).toEqual([]); 32 expect(formatError('boom')).toBe('boom'); 33 }); 34 35 test('formats a non-Error cause', () => { 36 const err = new Error('top level'); 37 err.cause = 'socket closed'; 38 39 expect(collectCauses(err)).toEqual(['socket closed']); 40 expect(formatError(err)).toBe([ 41 'top level', 42 ' caused by: socket closed', 43 ].join('\n')); 44 }); 45 46 test('stops on circular causes', () => { 47 const root = new Error('top level'); 48 const middle = new Error('middle'); 49 root.cause = middle; 50 middle.cause = root; 51 52 expect(collectCauses(root)).toEqual(['middle']); 53 expect(formatError(root)).toBe([ 54 'top level', 55 ' caused by: middle', 56 ].join('\n')); 57 }); 58 59 test('caps the cause list at 10 levels', () => { 60 const root = new Error('root'); 61 let current = root; 62 for (let i = 1; i <= 12; i += 1) { 63 const next = new Error(`cause ${i}`); 64 current.cause = next; 65 current = next; 66 } 67 68 expect(collectCauses(root)).toHaveLength(10); 69 expect(collectCauses(root)[0]).toBe('cause 1'); 70 expect(collectCauses(root)[9]).toBe('cause 10'); 71 }); 72 73 test('includes indented stack traces in verbose mode', () => { 74 const root = new Error('top level'); 75 const leaf = new Error('leaf'); 76 root.stack = 'Error: top level\nat top'; 77 leaf.stack = 'Error: leaf\nat leaf'; 78 root.cause = leaf; 79 80 expect(formatError(root, { verbose: true })).toBe([ 81 'top level', 82 ' Error: top level', 83 ' at top', 84 ' caused by: leaf', 85 ' Error: leaf', 86 ' at leaf', 87 ].join('\n')); 88 }); 89});