1
1
import { expect } from 'chai' ;
2
2
import { describe , it } from 'mocha' ;
3
3
4
+ import { expectPromise } from '../../__testUtils__/expectPromise' ;
5
+
4
6
import { mapAsyncIterable } from '../mapAsyncIterable' ;
5
7
6
8
/* eslint-disable @typescript-eslint/require-await */
@@ -209,14 +211,9 @@ describe('mapAsyncIterable', () => {
209
211
expect ( await doubles . next ( ) ) . to . deep . equal ( { value : 4 , done : false } ) ;
210
212
211
213
// Throw error
212
- let caughtError ;
213
- try {
214
- /* c8 ignore next 2 */
215
- await doubles . throw ( 'ouch' ) ;
216
- } catch ( e ) {
217
- caughtError = e ;
218
- }
219
- expect ( caughtError ) . to . equal ( 'ouch' ) ;
214
+ const message = 'allows throwing errors when mapping async iterable' ;
215
+ const thrown = doubles . throw ( new Error ( message ) ) ;
216
+ await expectPromise ( thrown ) . toRejectWith ( message ) ;
220
217
} ) ;
221
218
222
219
it ( 'passes through caught errors through async generators' , async ( ) => {
@@ -265,17 +262,7 @@ describe('mapAsyncIterable', () => {
265
262
done : false ,
266
263
} ) ;
267
264
268
- let caughtError ;
269
- try {
270
- /* c8 ignore next 2 */
271
- await doubles . next ( ) ;
272
- } catch ( e ) {
273
- caughtError = e ;
274
- }
275
-
276
- expect ( caughtError )
277
- . to . be . an . instanceOf ( Error )
278
- . with . property ( 'message' , 'Goodbye' ) ;
265
+ await expectPromise ( doubles . next ( ) ) . toRejectWith ( 'Goodbye' ) ;
279
266
} ) ;
280
267
281
268
async function testClosesSourceWithMapper < T > ( mapper : ( value : number ) => T ) {
@@ -297,17 +284,7 @@ describe('mapAsyncIterable', () => {
297
284
298
285
expect ( await throwOver1 . next ( ) ) . to . deep . equal ( { value : 1 , done : false } ) ;
299
286
300
- let expectedError ;
301
- try {
302
- /* c8 ignore next 2 */
303
- await throwOver1 . next ( ) ;
304
- } catch ( error ) {
305
- expectedError = error ;
306
- }
307
-
308
- expect ( expectedError )
309
- . to . be . an . instanceOf ( Error )
310
- . with . property ( 'message' , 'Cannot count to 2' ) ;
287
+ await expectPromise ( throwOver1 . next ( ) ) . toRejectWith ( 'Cannot count to 2' ) ;
311
288
312
289
expect ( await throwOver1 . next ( ) ) . to . deep . equal ( {
313
290
value : undefined ,
0 commit comments