catch-match 2.0.0 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -24
- package/dist/__tests__/main.test.d.ts +1 -0
- package/dist/__tests__/main.test.js +440 -0
- package/dist/__tests__/main.test.js.map +1 -0
- package/dist/main.d.ts +16 -11
- package/dist/main.js +82 -28
- package/dist/main.js.map +1 -1
- package/package.json +8 -2
- package/src/__tests__/main.test.ts +485 -209
- package/src/main.ts +101 -48
@@ -1,226 +1,502 @@
|
|
1
|
-
import $try from '../main';
|
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
|
-
test('should be match single exception', () => {
|
30
|
-
let matchReferenceError = jest.fn();
|
31
|
-
let matchArrayOfErrors = jest.fn();
|
32
|
-
let matchOther = jest.fn();
|
33
|
-
let matchFinally = jest.fn();
|
34
|
-
|
35
|
-
const result = $try(() => {
|
36
|
-
throw ReferenceError;
|
37
|
-
}).catch(ReferenceError, () => {
|
38
|
-
matchReferenceError();
|
39
|
-
}).catch([TypeError, SyntaxError], () => {
|
40
|
-
matchArrayOfErrors();
|
41
|
-
}).other((error) => {
|
42
|
-
matchOther(error);
|
43
|
-
}).finally(({ result, error }) => {
|
44
|
-
matchFinally({ result, error });
|
45
|
-
return result;
|
46
|
-
});
|
47
|
-
|
48
|
-
expect(matchReferenceError).toBeCalled();
|
49
|
-
expect(matchArrayOfErrors).not.toBeCalled();
|
50
|
-
expect(matchOther).not.toBeCalled();
|
51
|
-
expect(matchFinally).toBeCalled();
|
52
|
-
expect(result).toEqual({ error: ReferenceError, result: undefined });
|
53
|
-
});
|
54
|
-
|
55
|
-
test('should be match array of exceptions', () => {
|
56
|
-
let matchReferenceError = jest.fn();
|
57
|
-
let matchArrayOfErrors = jest.fn();
|
58
|
-
let matchOther = jest.fn();
|
59
|
-
let matchFinally = jest.fn();
|
60
|
-
|
61
|
-
const result = $try(() => {
|
62
|
-
throw SyntaxError;
|
63
|
-
}).catch(ReferenceError, () => {
|
64
|
-
matchReferenceError();
|
65
|
-
}).catch([TypeError, SyntaxError], () => {
|
66
|
-
matchArrayOfErrors();
|
67
|
-
}).other((error) => {
|
68
|
-
matchOther(error);
|
69
|
-
}).finally(({ result, error }) => {
|
70
|
-
matchFinally({ result, error });
|
71
|
-
return result;
|
72
|
-
});
|
73
|
-
|
74
|
-
expect(matchReferenceError).not.toBeCalled();
|
75
|
-
expect(matchArrayOfErrors).toBeCalled();
|
76
|
-
expect(matchOther).not.toBeCalled();
|
77
|
-
expect(matchFinally).toBeCalled();
|
78
|
-
expect(result).toEqual({ error: SyntaxError, result: undefined });
|
79
|
-
});
|
80
|
-
|
81
|
-
test('should be match any type of exception', () => {
|
82
|
-
let matchReferenceError = jest.fn();
|
83
|
-
let matchArrayOfErrors = jest.fn();
|
84
|
-
let matchOther = jest.fn();
|
85
|
-
let matchFinally = jest.fn();
|
86
|
-
|
87
|
-
const result = $try(() => {
|
88
|
-
throw 'error';
|
89
|
-
}).catch(ReferenceError, () => {
|
90
|
-
matchReferenceError();
|
91
|
-
}).catch([TypeError, SyntaxError], () => {
|
92
|
-
matchArrayOfErrors();
|
93
|
-
}).other((error) => {
|
94
|
-
matchOther(error);
|
95
|
-
}).finally(({ result, error }) => {
|
96
|
-
matchFinally({ result, error });
|
97
|
-
return result;
|
98
|
-
});
|
99
|
-
|
100
|
-
expect(matchReferenceError).not.toBeCalled();
|
101
|
-
expect(matchArrayOfErrors).not.toBeCalled();
|
102
|
-
expect(matchOther).toBeCalled();
|
103
|
-
expect(matchFinally).toBeCalled();
|
104
|
-
expect(result).toEqual({ error: 'error', result: undefined });
|
105
|
-
});
|
106
|
-
|
107
|
-
test('should be match custom exceptions', () => {
|
108
|
-
let matchCustomError = jest.fn();
|
109
|
-
let matchArrayOfErrors = jest.fn();
|
110
|
-
let matchOther = jest.fn();
|
111
|
-
let matchFinally = jest.fn();
|
112
|
-
|
113
|
-
class CustomError extends Error {
|
114
|
-
}
|
115
|
-
|
116
|
-
const result = $try(() => {
|
117
|
-
throw CustomError;
|
118
|
-
}).catch(CustomError, () => {
|
119
|
-
matchCustomError();
|
120
|
-
}).catch([TypeError, SyntaxError], () => {
|
121
|
-
matchArrayOfErrors();
|
122
|
-
}).other((error) => {
|
123
|
-
matchOther(error);
|
124
|
-
}).finally(({ result, error }) => {
|
125
|
-
matchFinally({ result, error });
|
126
|
-
return result;
|
127
|
-
});
|
128
|
-
|
129
|
-
expect(matchCustomError).toBeCalled();
|
130
|
-
expect(matchArrayOfErrors).not.toBeCalled();
|
131
|
-
expect(matchOther).not.toBeCalled();
|
132
|
-
expect(matchFinally).toBeCalled();
|
133
|
-
expect(result).toEqual({ error: CustomError, result: undefined });
|
134
|
-
});
|
135
|
-
|
136
|
-
test('should be return result from `finally` branch without `other`', () => {
|
137
|
-
let matchCustomError = jest.fn();
|
138
|
-
let matchArrayOfErrors = jest.fn();
|
139
|
-
let matchOther = jest.fn();
|
140
|
-
let matchFinally = jest.fn();
|
141
|
-
|
142
|
-
class CustomError extends Error {
|
143
|
-
}
|
144
|
-
|
145
|
-
const result = $try(() => {
|
146
|
-
throw CustomError;
|
147
|
-
}).catch([TypeError, SyntaxError], () => {
|
148
|
-
matchArrayOfErrors();
|
149
|
-
}).finally(({ result, error }) => {
|
150
|
-
matchFinally({ result, error });
|
151
|
-
return result;
|
152
|
-
});
|
153
|
-
|
154
|
-
expect(matchCustomError).not.toBeCalled();
|
155
|
-
expect(matchArrayOfErrors).not.toBeCalled();
|
156
|
-
expect(matchOther).not.toBeCalled();
|
157
|
-
expect(matchFinally).toBeCalled();
|
158
|
-
expect(result).toEqual({ error: CustomError, result: undefined });
|
159
|
-
});
|
160
|
-
|
161
|
-
|
162
|
-
test('should be return result from `catch` branch', () => {
|
163
|
-
let matchCustomError = jest.fn();
|
164
|
-
let matchArrayOfErrors = jest.fn();
|
165
|
-
let matchOther = jest.fn();
|
166
|
-
let matchFinally = jest.fn();
|
1
|
+
import $try, { PromisedTryReturn } from '../main';
|
2
|
+
|
3
|
+
describe('sync version', () => {
|
4
|
+
test('should be return result from `finally` branch', () => {
|
5
|
+
let matchReferenceError = jest.fn();
|
6
|
+
let matchArrayOfErrors = jest.fn();
|
7
|
+
let matchOther = jest.fn();
|
8
|
+
let matchFinally = jest.fn();
|
9
|
+
|
10
|
+
const result = $try(() => {
|
11
|
+
return [1, 2, 3];
|
12
|
+
}).catch(ReferenceError, () => {
|
13
|
+
matchReferenceError();
|
14
|
+
}).catch([TypeError, SyntaxError], (error) => {
|
15
|
+
matchArrayOfErrors(error);
|
16
|
+
}).other((error) => {
|
17
|
+
matchOther(error);
|
18
|
+
}).finally(({ value, error }) => { //?
|
19
|
+
matchFinally({ value, error });
|
20
|
+
});
|
21
|
+
|
22
|
+
expect(matchReferenceError).not.toBeCalled();
|
23
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
24
|
+
expect(matchOther).not.toBeCalled();
|
25
|
+
expect(matchFinally).toBeCalled();
|
26
|
+
expect(result).toMatchObject({ error: undefined, value: [1, 2, 3] });
|
27
|
+
});
|
167
28
|
|
168
|
-
|
169
|
-
|
29
|
+
test('should be match single exception', () => {
|
30
|
+
let matchReferenceError = jest.fn();
|
31
|
+
let matchArrayOfErrors = jest.fn();
|
32
|
+
let matchOther = jest.fn();
|
33
|
+
let matchFinally = jest.fn();
|
34
|
+
|
35
|
+
const result = $try(() => {
|
36
|
+
throw ReferenceError;
|
37
|
+
}).catch(ReferenceError, () => {
|
38
|
+
matchReferenceError();
|
39
|
+
}).catch([TypeError, SyntaxError], (error) => {
|
40
|
+
matchArrayOfErrors(error);
|
41
|
+
}).other((error) => {
|
42
|
+
matchOther(error);
|
43
|
+
}).finally(({ value, error }) => {
|
44
|
+
matchFinally({ value, error });
|
45
|
+
return value; // ?
|
46
|
+
});
|
47
|
+
|
48
|
+
expect(matchReferenceError).toBeCalled();
|
49
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
50
|
+
expect(matchOther).not.toBeCalled();
|
51
|
+
expect(matchFinally).toBeCalled();
|
52
|
+
expect(result).toMatchObject({ error: ReferenceError, value: undefined });
|
53
|
+
});
|
170
54
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
55
|
+
test('should be match array of exceptions', () => {
|
56
|
+
let matchReferenceError = jest.fn();
|
57
|
+
let matchArrayOfErrors = jest.fn();
|
58
|
+
let matchOther = jest.fn();
|
59
|
+
let matchFinally = jest.fn();
|
60
|
+
|
61
|
+
const result = $try(() => {
|
62
|
+
throw SyntaxError;
|
63
|
+
}).catch(ReferenceError, () => {
|
64
|
+
matchReferenceError();
|
65
|
+
}).catch([TypeError, SyntaxError], (error) => {
|
66
|
+
matchArrayOfErrors(error);
|
67
|
+
}).other((error) => {
|
68
|
+
matchOther(error);
|
69
|
+
}).finally(({ value, error }) => {
|
70
|
+
matchFinally({ value, error });
|
71
|
+
return value; // ?
|
72
|
+
});
|
73
|
+
|
74
|
+
expect(matchReferenceError).not.toBeCalled();
|
75
|
+
expect(matchArrayOfErrors).toBeCalledWith(SyntaxError);
|
76
|
+
expect(matchOther).not.toBeCalled();
|
77
|
+
expect(matchFinally).toBeCalled();
|
78
|
+
expect(result).toMatchObject({ error: SyntaxError, value: undefined });
|
175
79
|
});
|
176
80
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
81
|
+
test('should be match any type of exception', () => {
|
82
|
+
let matchReferenceError = jest.fn();
|
83
|
+
let matchArrayOfErrors = jest.fn();
|
84
|
+
let matchOther = jest.fn();
|
85
|
+
let matchFinally = jest.fn();
|
86
|
+
|
87
|
+
const result = $try(() => {
|
88
|
+
throw 'error';
|
89
|
+
}).catch(ReferenceError, () => {
|
90
|
+
matchReferenceError();
|
91
|
+
}).catch([TypeError, SyntaxError], (error) => {
|
92
|
+
matchArrayOfErrors(error);
|
93
|
+
}).other((error) => {
|
94
|
+
matchOther(error);
|
95
|
+
}).finally(({ value, error }) => {
|
96
|
+
matchFinally({ value, error });
|
97
|
+
return value; // ?
|
98
|
+
});
|
99
|
+
|
100
|
+
expect(matchReferenceError).not.toBeCalled();
|
101
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
102
|
+
expect(matchOther).toBeCalled();
|
103
|
+
expect(matchFinally).toBeCalled();
|
104
|
+
expect(result).toMatchObject({ error: 'error', value: undefined });
|
105
|
+
});
|
183
106
|
|
107
|
+
test('should be match custom exceptions', () => {
|
108
|
+
let matchCustomError = jest.fn();
|
109
|
+
let matchArrayOfErrors = jest.fn();
|
110
|
+
let matchOther = jest.fn();
|
111
|
+
let matchFinally = jest.fn();
|
112
|
+
|
113
|
+
class CustomError extends Error {
|
114
|
+
}
|
115
|
+
|
116
|
+
const result = $try(() => {
|
117
|
+
throw CustomError;
|
118
|
+
}).catch(CustomError, () => {
|
119
|
+
matchCustomError();
|
120
|
+
}).catch([TypeError, SyntaxError], (error) => {
|
121
|
+
matchArrayOfErrors(error);
|
122
|
+
}).other((error) => {
|
123
|
+
matchOther(error);
|
124
|
+
}).finally(({ value, error }) => {
|
125
|
+
matchFinally({ value, error });
|
126
|
+
});
|
127
|
+
|
128
|
+
expect(matchCustomError).toBeCalled();
|
129
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
130
|
+
expect(matchOther).not.toBeCalled();
|
131
|
+
expect(matchFinally).toBeCalled();
|
132
|
+
expect(result).toMatchObject({ error: CustomError, value: undefined });
|
133
|
+
});
|
184
134
|
|
185
|
-
test('should be return result from `
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
135
|
+
test('should be return result from `finally` branch without `other`', () => {
|
136
|
+
let matchCustomError = jest.fn();
|
137
|
+
let matchArrayOfErrors = jest.fn();
|
138
|
+
let matchOther = jest.fn();
|
139
|
+
let matchFinally = jest.fn();
|
140
|
+
|
141
|
+
class CustomError extends Error {
|
142
|
+
}
|
143
|
+
|
144
|
+
const result = $try(() => {
|
145
|
+
throw CustomError;
|
146
|
+
}).catch([TypeError, SyntaxError], (error) => {
|
147
|
+
matchArrayOfErrors(error);
|
148
|
+
}).finally(({ value, error }) => {
|
149
|
+
matchFinally({ value, error });
|
150
|
+
return value; // ?
|
151
|
+
});
|
152
|
+
|
153
|
+
expect(matchCustomError).not.toBeCalled();
|
154
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
155
|
+
expect(matchOther).not.toBeCalled();
|
156
|
+
expect(matchFinally).toBeCalled();
|
157
|
+
expect(result).toMatchObject({ error: CustomError, value: undefined });
|
158
|
+
});
|
190
159
|
|
191
|
-
|
192
|
-
|
160
|
+
test('should be return result from `catch` branch', () => {
|
161
|
+
let matchCustomError = jest.fn();
|
162
|
+
let matchArrayOfErrors = jest.fn();
|
163
|
+
let matchOther = jest.fn();
|
164
|
+
let matchFinally = jest.fn();
|
165
|
+
|
166
|
+
class CustomError extends Error {
|
167
|
+
}
|
168
|
+
|
169
|
+
const result = $try(() => {
|
170
|
+
throw CustomError;
|
171
|
+
}).catch([TypeError, SyntaxError], (error) => {
|
172
|
+
matchArrayOfErrors(error);
|
173
|
+
});
|
174
|
+
|
175
|
+
expect(matchCustomError).not.toBeCalled();
|
176
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
177
|
+
expect(matchOther).not.toBeCalled();
|
178
|
+
expect(matchFinally).not.toBeCalled();
|
179
|
+
expect(result).toMatchObject({ error: CustomError, value: undefined });
|
180
|
+
});
|
193
181
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
matchOther(
|
182
|
+
test('should be return result from `other` branch', () => {
|
183
|
+
let matchCustomError = jest.fn();
|
184
|
+
let matchArrayOfErrors = jest.fn();
|
185
|
+
let matchOther = jest.fn();
|
186
|
+
let matchFinally = jest.fn();
|
187
|
+
|
188
|
+
class CustomError extends Error {
|
189
|
+
}
|
190
|
+
|
191
|
+
const result = $try(() => {
|
192
|
+
throw CustomError;
|
193
|
+
}).other((error) => {
|
194
|
+
matchOther(error);
|
195
|
+
});
|
196
|
+
|
197
|
+
expect(matchCustomError).not.toBeCalled();
|
198
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
199
|
+
expect(matchOther).toBeCalledWith(CustomError);
|
200
|
+
expect(matchFinally).not.toBeCalled();
|
201
|
+
expect(result).toMatchObject({ error: CustomError, value: undefined });
|
198
202
|
});
|
199
203
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
204
|
+
test('should be return result without branches', () => {
|
205
|
+
let matchCustomError = jest.fn();
|
206
|
+
let matchArrayOfErrors = jest.fn();
|
207
|
+
let matchOther = jest.fn();
|
208
|
+
let matchFinally = jest.fn();
|
209
|
+
|
210
|
+
const result = $try(() => {
|
211
|
+
throw SyntaxError;
|
212
|
+
});
|
213
|
+
|
214
|
+
expect(matchCustomError).not.toBeCalled();
|
215
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
216
|
+
expect(matchOther).not.toBeCalled();
|
217
|
+
expect(matchFinally).not.toBeCalled();
|
218
|
+
expect(result).toMatchObject({ error: SyntaxError, value: undefined });
|
219
|
+
});
|
205
220
|
});
|
206
221
|
|
222
|
+
/**
|
223
|
+
* async tests
|
224
|
+
*/
|
225
|
+
describe('promised version', () => {
|
226
|
+
test('resolve try', async () => {
|
227
|
+
let matchCustomError = jest.fn();
|
228
|
+
let matchArrayOfErrors = jest.fn();
|
229
|
+
let matchOther = jest.fn();
|
230
|
+
let matchFinally = jest.fn();
|
231
|
+
|
232
|
+
const result = $try(() => {
|
233
|
+
return Promise.resolve(42);
|
234
|
+
});
|
235
|
+
|
236
|
+
await result;
|
237
|
+
|
238
|
+
expect(matchCustomError).not.toBeCalled();
|
239
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
240
|
+
expect(matchOther).not.toBeCalled();
|
241
|
+
expect(matchFinally).not.toBeCalled();
|
242
|
+
|
243
|
+
expect(result).toBeInstanceOf(Promise);
|
244
|
+
expect(await result).toEqual({
|
245
|
+
value: 42,
|
246
|
+
error: undefined,
|
247
|
+
});
|
248
|
+
});
|
249
|
+
test('reject try', async () => {
|
250
|
+
let matchCustomError = jest.fn();
|
251
|
+
let matchArrayOfErrors = jest.fn();
|
252
|
+
let matchOther = jest.fn();
|
253
|
+
let matchFinally = jest.fn();
|
254
|
+
|
255
|
+
const result = $try(() => {
|
256
|
+
return Promise.reject(42);
|
257
|
+
});
|
258
|
+
|
259
|
+
expect(matchCustomError).not.toBeCalled();
|
260
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
261
|
+
expect(matchOther).not.toBeCalled();
|
262
|
+
expect(matchFinally).not.toBeCalled();
|
263
|
+
|
264
|
+
expect(result).toBeInstanceOf(Promise);
|
265
|
+
expect(await result).toEqual({
|
266
|
+
value: undefined,
|
267
|
+
error: 42,
|
268
|
+
});
|
269
|
+
});
|
270
|
+
test('throw try', async () => {
|
271
|
+
const result = $try(() => {
|
272
|
+
return new Promise(() => {
|
273
|
+
throw new Error('error');
|
274
|
+
});
|
275
|
+
});
|
276
|
+
|
277
|
+
expect(result).toBeInstanceOf(Promise);
|
278
|
+
|
279
|
+
expect(await result).toEqual({
|
280
|
+
value: undefined,
|
281
|
+
error: Error('error'),
|
282
|
+
});
|
283
|
+
});
|
207
284
|
|
208
|
-
test('
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
285
|
+
test('resolve try.catch', async () => {
|
286
|
+
let matchError = jest.fn();
|
287
|
+
let matchSyntaxError = jest.fn();
|
288
|
+
let matchOther = jest.fn();
|
289
|
+
|
290
|
+
const result = $try(() => {
|
291
|
+
return Promise.resolve(42);
|
292
|
+
}).catch(Error, () => {
|
293
|
+
matchError();
|
294
|
+
}).catch(SyntaxError, () => {
|
295
|
+
matchSyntaxError();
|
296
|
+
}).other((error) => {
|
297
|
+
matchOther(error);
|
298
|
+
});
|
299
|
+
|
300
|
+
expect(matchError).not.toBeCalled();
|
301
|
+
expect(matchSyntaxError).not.toBeCalled();
|
302
|
+
expect(matchOther).not.toBeCalled();
|
303
|
+
|
304
|
+
expect(result).toBeInstanceOf(Promise);
|
305
|
+
expect(await result).toEqual({
|
306
|
+
value: 42,
|
307
|
+
error: undefined,
|
308
|
+
});
|
309
|
+
});
|
310
|
+
test('reject try.catch', async () => {
|
311
|
+
let matchError = jest.fn();
|
312
|
+
let matchOther = jest.fn();
|
313
|
+
|
314
|
+
const result = $try(() => {
|
315
|
+
return Promise.reject(123);
|
316
|
+
}).catch(Error, () => {
|
317
|
+
matchOther();
|
318
|
+
}).catch(123, () => {
|
319
|
+
matchError();
|
320
|
+
}).catch('error', () => {
|
321
|
+
matchOther();
|
322
|
+
});
|
323
|
+
|
324
|
+
await result; //?
|
325
|
+
|
326
|
+
expect(matchOther).not.toBeCalled();
|
327
|
+
expect(matchError).toBeCalled();
|
328
|
+
|
329
|
+
expect(result).toBeInstanceOf(Promise);
|
330
|
+
expect(await result).toEqual({
|
331
|
+
value: undefined,
|
332
|
+
error: 123,
|
333
|
+
});
|
334
|
+
});
|
335
|
+
test('throw try.catch', async () => {
|
336
|
+
let matchError = jest.fn();
|
337
|
+
let matchOther = jest.fn();
|
338
|
+
|
339
|
+
const result = $try(() => {
|
340
|
+
return new Promise(() => {
|
341
|
+
throw 'error';
|
342
|
+
});
|
343
|
+
}).catch(Error, () => {
|
344
|
+
matchOther();
|
345
|
+
}).catch(123, () => {
|
346
|
+
matchError();
|
347
|
+
}).catch('error', () => {
|
348
|
+
matchOther();
|
349
|
+
});
|
350
|
+
|
351
|
+
expect(result).toBeInstanceOf(Promise);
|
352
|
+
expect(await result).toEqual({
|
353
|
+
value: undefined,
|
354
|
+
error: 'error',
|
355
|
+
});
|
356
|
+
|
357
|
+
expect(matchOther).toBeCalledTimes(1);
|
358
|
+
expect(matchError).not.toBeCalled();
|
359
|
+
});
|
216
360
|
|
217
|
-
|
218
|
-
|
361
|
+
test('resolve try.other', async () => {
|
362
|
+
let matchCustomError = jest.fn();
|
363
|
+
let matchArrayOfErrors = jest.fn();
|
364
|
+
let matchOther = jest.fn();
|
365
|
+
let matchFinally = jest.fn();
|
366
|
+
|
367
|
+
const result = $try(() => {
|
368
|
+
return Promise.resolve('ok');
|
369
|
+
}).other(({ value, error }) => {
|
370
|
+
matchOther({ value, error });
|
371
|
+
});
|
372
|
+
|
373
|
+
expect(result).toBeInstanceOf(Promise);
|
374
|
+
expect(await result).toEqual({
|
375
|
+
value: 'ok',
|
376
|
+
error: undefined,
|
377
|
+
});
|
378
|
+
|
379
|
+
expect(matchCustomError).not.toBeCalled();
|
380
|
+
expect(matchArrayOfErrors).not.toBeCalled();
|
381
|
+
expect(matchOther).not.toBeCalled();
|
382
|
+
expect(matchFinally).not.toBeCalled();
|
383
|
+
});
|
384
|
+
test('reject try.other', async () => {
|
385
|
+
let matchError = jest.fn();
|
386
|
+
let matchOther = jest.fn();
|
387
|
+
|
388
|
+
const result = $try(() => {
|
389
|
+
return Promise.reject('error');
|
390
|
+
}).catch('error1', () => {
|
391
|
+
matchError();
|
392
|
+
}).catch('error2', () => {
|
393
|
+
matchError();
|
394
|
+
}).other((error) => {
|
395
|
+
matchOther(error);
|
396
|
+
});
|
397
|
+
|
398
|
+
expect(result).toBeInstanceOf(Promise);
|
399
|
+
expect(await result).toEqual({
|
400
|
+
value: undefined,
|
401
|
+
error: 'error',
|
402
|
+
});
|
403
|
+
|
404
|
+
expect(matchError).not.toBeCalled();
|
405
|
+
expect(matchOther).toBeCalledWith('error');
|
406
|
+
});
|
407
|
+
test('throw try.other', async () => {
|
408
|
+
let matchError = jest.fn();
|
409
|
+
let matchOther = jest.fn();
|
410
|
+
|
411
|
+
const result = $try(() => {
|
412
|
+
return new Promise(() => {
|
413
|
+
throw 'error';
|
414
|
+
});
|
415
|
+
}).catch('error1', () => {
|
416
|
+
matchError();
|
417
|
+
}).catch('error2', () => {
|
418
|
+
matchError();
|
419
|
+
}).other((error) => {
|
420
|
+
matchOther(error);
|
421
|
+
});
|
422
|
+
|
423
|
+
expect(result).toBeInstanceOf(Promise);
|
424
|
+
expect(await result).toEqual({
|
425
|
+
value: undefined,
|
426
|
+
error: 'error',
|
427
|
+
});
|
428
|
+
|
429
|
+
expect(matchError).not.toBeCalled();
|
430
|
+
expect(matchOther).toBeCalledWith('error');
|
219
431
|
});
|
220
432
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
433
|
+
test('when throw should be match array or exception', async () => {
|
434
|
+
let matchSingleError = jest.fn();
|
435
|
+
let matchArrayError = jest.fn();
|
436
|
+
let matchOther = jest.fn();
|
437
|
+
let matchFinally = jest.fn();
|
438
|
+
|
439
|
+
const attach = (ctx: PromisedTryReturn<any>) => {
|
440
|
+
return ctx.catch(SyntaxError, () => {
|
441
|
+
matchSingleError(SyntaxError);
|
442
|
+
}).catch([ReferenceError, SyntaxError], (error) => {
|
443
|
+
matchArrayError(error);
|
444
|
+
}).other((error) => {
|
445
|
+
matchOther(error);
|
446
|
+
}).finally(({ value, error }) => {
|
447
|
+
matchFinally({ value, error });
|
448
|
+
});
|
449
|
+
};
|
450
|
+
|
451
|
+
const testSingle = $try(() => new Promise(() => {
|
452
|
+
throw SyntaxError;
|
453
|
+
}));
|
454
|
+
|
455
|
+
const testArray = $try(() => new Promise(() => {
|
456
|
+
throw ReferenceError;
|
457
|
+
}));
|
458
|
+
|
459
|
+
const testOther = $try(() => new Promise(() => {
|
460
|
+
throw TypeError;
|
461
|
+
}));
|
462
|
+
|
463
|
+
const testResolve = $try(() => new Promise((resolve) => {
|
464
|
+
resolve('ok');
|
465
|
+
}));
|
466
|
+
|
467
|
+
const testReject = $try(() => new Promise((_, reject) => {
|
468
|
+
reject('reject');
|
469
|
+
}));
|
470
|
+
|
471
|
+
const resultSingle = attach(testSingle);
|
472
|
+
const resultArray = attach(testArray);
|
473
|
+
const resultOther = attach(testOther);
|
474
|
+
const resultResolve = attach(testResolve);
|
475
|
+
const resultReject = attach(testReject);
|
476
|
+
|
477
|
+
expect(resultSingle).toBeInstanceOf(Promise);
|
478
|
+
expect(resultArray).toBeInstanceOf(Promise);
|
479
|
+
expect(resultOther).toBeInstanceOf(Promise);
|
480
|
+
expect(resultResolve).toBeInstanceOf(Promise);
|
481
|
+
expect(resultReject).toBeInstanceOf(Promise);
|
482
|
+
|
483
|
+
expect(await resultSingle).toEqual({
|
484
|
+
value: undefined,
|
485
|
+
error: SyntaxError,
|
486
|
+
});
|
487
|
+
expect(await resultArray).toEqual({
|
488
|
+
value: undefined,
|
489
|
+
error: ReferenceError,
|
490
|
+
});
|
491
|
+
expect(await resultOther).toEqual({
|
492
|
+
value: undefined,
|
493
|
+
error: TypeError,
|
494
|
+
});
|
495
|
+
expect(await resultResolve).toEqual({
|
496
|
+
value: 'ok',
|
497
|
+
});
|
498
|
+
expect(await resultReject).toEqual({
|
499
|
+
error: 'reject',
|
500
|
+
});
|
501
|
+
});
|
226
502
|
});
|