deno.land / x / rambda@v9.1.1 / source / tryCatch.spec.js

tryCatch.spec.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { tryCatch as tryCatchRamda } from 'ramda'
import { compareCombinations } from './_internals/testUtils.js'import { prop } from './prop.js'import { tryCatch } from './tryCatch.js'
test('happy', () => { const fn = () => { throw new Error('foo') } const result = tryCatch(fn, () => true)() expect(result).toBeTrue()})
test('when fallback is used', () => { const fn = x => x.x
expect(tryCatch(fn, false)(null)).toBeFalse()})
test('with json parse', () => { const good = () => JSON.parse(JSON.stringify({ a : 1 })) const bad = () => JSON.parse('a{a')
expect(tryCatch(good, 1)()).toEqual({ a : 1 }) expect(tryCatch(bad, 1)()).toBe(1)})
test('when fallback is function', () => { const fn = x => x.x
expect(tryCatch(fn, () => 1)(null)).toBe(1)})
test('when fn is used', () => { const fn = prop('x')
expect(tryCatch(fn, false)({})).toBeUndefined() expect(tryCatch(fn, false)({ x : 1 })).toBe(1)})
test('fallback receives error object and all initial inputs', () => { function thrower( a, b, c ){ void c throw new Error('throwerError') }
function catchFn( e, a, b, c ){ return [ e.message, a, b, c ].join('|') }
const willThrow = tryCatch(thrower, catchFn) const result = willThrow( 'A', 'B', 'C' ) expect(result).toBe('throwerError|A|B|C')})
test('fallback receives error object', () => { function throwFn(){ throw new Error(10) }
function eCatcher( e, a, b ){ return e.message }
const willThrow = tryCatch(throwFn, eCatcher) expect(willThrow([])).toBe('10') expect(willThrow([ {}, {}, {} ])).toBe('10')})
const possibleFns = [ null, () => 1, () => 0, () => JSON.parse('{a:1'), () => { const x = {}
return x.x }, x => x.foo, () => { throw new Error('foo') },]
const possibleCatchers = [ null, e => e.message.length, (e, ...inputs) => `${ e.message.length } ${ inputs.length }`, () => { throw new Error('bar') },]
const possibleInputs = [ null, {}, { foo : 1 } ]
describe('brute force', () => { compareCombinations({ returnsFunctionFlag : true, firstInput : possibleFns, callback : errorsCounters => { expect(errorsCounters).toMatchInlineSnapshot(` { "ERRORS_MESSAGE_MISMATCH": 0, "ERRORS_TYPE_MISMATCH": 12, "RESULTS_MISMATCH": 0, "SHOULD_NOT_THROW": 0, "SHOULD_THROW": 7, "TOTAL_TESTS": 84, } `) }, secondInput : possibleCatchers, thirdInput : possibleInputs, fn : tryCatch, fnRamda : tryCatchRamda, })})
rambda

Version Info

Tagged at
2 months ago