| 1 |
import { test } from 'tap' |
| 2 |
import { parse, stringify } from 'yaml' |
| 3 |
|
| 4 |
import { inspect } from 'node:util' |
| 5 |
|
| 6 |
import { error } from '.' |
| 7 |
test('parse basic', t => { |
| 8 |
const res: RangeError = parse( |
| 9 |
`!error |
| 10 |
name: RangeError |
| 11 |
message: hello |
| 12 |
stack: |2 |
| 13 |
RangeError: hello |
| 14 |
at some-file.js:1:2 |
| 15 |
at Object.someMethod (/path/to/other-file.js:123:420) |
| 16 |
`, |
| 17 |
{ customTags: [error] } |
| 18 |
) |
| 19 |
t.type(res, RangeError) |
| 20 |
t.match(res, { |
| 21 |
name: 'RangeError', |
| 22 |
message: 'hello', |
| 23 |
stack: `RangeError: hello |
| 24 |
at some-file.js:1:2 |
| 25 |
at Object.someMethod (/path/to/other-file.js:123:420) |
| 26 |
` |
| 27 |
}) |
| 28 |
t.end() |
| 29 |
}) |
| 30 |
|
| 31 |
test('parse custom named error', t => { |
| 32 |
const res: Error = parse( |
| 33 |
`!error |
| 34 |
name: CustomErrorSubclass |
| 35 |
message: custom message |
| 36 |
otherProperty: 123 |
| 37 |
`, |
| 38 |
{ customTags: [error] } |
| 39 |
) |
| 40 |
t.type(res, Error) |
| 41 |
t.match(res, { |
| 42 |
name: 'CustomErrorSubclass', |
| 43 |
message: 'custom message', |
| 44 |
// if no stack in the yaml, none in the error object either |
| 45 |
stack: undefined, |
| 46 |
otherProperty: 123 |
| 47 |
}) |
| 48 |
t.end() |
| 49 |
}) |
| 50 |
|
| 51 |
test('stringify basic', t => { |
| 52 |
const e = new Error('message') |
| 53 |
const res = stringify(e, { customTags: [error] }) |
| 54 |
t.match(res, /^!error\nname: Error\nmessage: message\nstack: /) |
| 55 |
t.end() |
| 56 |
}) |
| 57 |
|
| 58 |
test('stringify with custom inspect', t => { |
| 59 |
const e = Object.assign(new URIError('blah'), { |
| 60 |
// no real need to do this, stacks are just annoying to test, |
| 61 |
// and we already verified above that gets a stack in the yaml. |
| 62 |
stack: undefined, |
| 63 |
cause: { foo: 'bar' }, |
| 64 |
a: true, |
| 65 |
b: false, |
| 66 |
c: 123, |
| 67 |
[inspect.custom]: () => { |
| 68 |
return { a: 1, b: 2 } |
| 69 |
} |
| 70 |
}) |
| 71 |
const res = stringify(e, { customTags: [error] }) |
| 72 |
t.equal( |
| 73 |
res, |
| 74 |
`!error |
| 75 |
name: URIError |
| 76 |
message: blah |
| 77 |
cause: |
| 78 |
foo: bar |
| 79 |
a: 1 |
| 80 |
b: 2 |
| 81 |
` |
| 82 |
) |
| 83 |
t.end() |
| 84 |
}) |
| 85 |
|
| 86 |
test('stringify with custom toJSON', t => { |
| 87 |
const e = Object.assign(new URIError('blah'), { |
| 88 |
// no real need to do this, stacks are just annoying to test, |
| 89 |
// and we already verified above that gets a stack in the yaml. |
| 90 |
stack: undefined, |
| 91 |
a: true, |
| 92 |
b: false, |
| 93 |
c: 123, |
| 94 |
toJSON: () => { |
| 95 |
return { a: 1, b: 2 } |
| 96 |
} |
| 97 |
}) |
| 98 |
const res = stringify(e, { customTags: [error] }) |
| 99 |
t.equal( |
| 100 |
res, |
| 101 |
`!error |
| 102 |
name: URIError |
| 103 |
message: blah |
| 104 |
a: 1 |
| 105 |
b: 2 |
| 106 |
` |
| 107 |
) |
| 108 |
t.end() |
| 109 |
}) |
| 110 |
|
| 111 |
test('supports all known error types', t => { |
| 112 |
const types: { new (m: string): Error }[] = [ |
| 113 |
EvalError, |
| 114 |
RangeError, |
| 115 |
ReferenceError, |
| 116 |
SyntaxError, |
| 117 |
TypeError, |
| 118 |
URIError, |
| 119 |
Error |
| 120 |
] |
| 121 |
t.plan(types.length) |
| 122 |
for (const Cls of types) { |
| 123 |
t.test(Cls.name, t => { |
| 124 |
const e = new Cls('message') |
| 125 |
const res = stringify(e, { customTags: [error] }) |
| 126 |
t.match( |
| 127 |
res, |
| 128 |
`!error |
| 129 |
name: ${Cls.name} |
| 130 |
message: message |
| 131 |
` |
| 132 |
) |
| 133 |
const parsed = parse(res, { customTags: [error] }) |
| 134 |
t.type(parsed, Cls) |
| 135 |
t.end() |
| 136 |
}) |
| 137 |
} |
| 138 |
}) |