|
1 | 1 | import type { Simplify } from 'type-fest'; |
2 | 2 | import { describe, expect, expectTypeOf, it, test } from 'vitest'; |
3 | 3 |
|
4 | | -import { BaseException, ExceptionBuilder, OutOfRangeException, parseStack, ValueException } from '../exception.js'; |
| 4 | +import { |
| 5 | + BaseException, |
| 6 | + errorToJSON, |
| 7 | + ExceptionBuilder, |
| 8 | + OutOfRangeException, |
| 9 | + parseStack, |
| 10 | + RuntimeException, |
| 11 | + ValueException |
| 12 | +} from '../exception.js'; |
5 | 13 | import { Err } from '../vendor/neverthrow.js'; |
6 | 14 |
|
7 | 15 | import type { ExceptionConstructor } from '../exception.js'; |
@@ -194,7 +202,31 @@ describe('parseStack', () => { |
194 | 202 | it('should return the same value, in terms of value, whether called with an error or a string', () => { |
195 | 203 | const error = new Error(); |
196 | 204 | const r1 = parseStack(error); |
197 | | - const r2 = parseStack(error.stack!); |
| 205 | + const r2 = parseStack(error.stack); |
198 | 206 | expect(r1).toStrictEqual(r2); |
199 | 207 | }); |
200 | 208 | }); |
| 209 | + |
| 210 | +describe('errorToJSON', () => { |
| 211 | + it('should return the expected output', () => { |
| 212 | + const cause = new RuntimeException('Something else went wrong', { |
| 213 | + details: { |
| 214 | + foo: true |
| 215 | + } |
| 216 | + }); |
| 217 | + const error = new Error('Something went wrong', { cause }); |
| 218 | + expect(JSON.parse(errorToJSON(error))).toStrictEqual({ |
| 219 | + cause: { |
| 220 | + details: { |
| 221 | + foo: true |
| 222 | + }, |
| 223 | + message: 'Something else went wrong', |
| 224 | + name: 'RuntimeException', |
| 225 | + stack: expect.toSatisfy((arg) => Array.isArray(arg) && arg.every((item) => typeof item === 'string')) |
| 226 | + }, |
| 227 | + message: 'Something went wrong', |
| 228 | + name: 'Error', |
| 229 | + stack: expect.toSatisfy((arg) => Array.isArray(arg) && arg.every((item) => typeof item === 'string')) |
| 230 | + }); |
| 231 | + }); |
| 232 | +}); |
0 commit comments