Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
Jest no puede probar el error de lanzamiento en la función asíncrona del mapa

Tengo el siguiente código de validación en una clase de servicio:

 order.items.map(async (item) => { const itemResult = await this.itemRepository.getById(item.id) if(itemResult == undefined && itemResult == null){ throw new NotFoundError('Item not found in database') } })

Y creé la siguiente prueba en Jest con una orden que no tiene un artículo en la base de datos:

 it('Should validate a Order', async () => { const item: Item = { id: '2', name: 'Item do not exist', price: '20.0' } const order = { id: uuidv4(), amount: '100.0', items: [item], created_at: new Date() } as Order await expect(async () => { orderService.createOrder(order) }).toThrowError() })

Pero cuando ejecuto Jest, la prueba falla y se muestra la terminal:

 RUNS src/core/service/OrderService.spec.ts /home/user/repo/projetc/src/core/service/OrderService.ts:1162 throw new NotFoundError_1.NotFoundError('Item not found in database'); ^ NotFoundError: Item not found in database at OrderService.<anonymous> (/home/user/repo/projetc/src/core/service/OrderService.ts:1162:19) at Generator.next (<anonymous>) at fulfilled (/home/user/repo/projetc/src/core/service/OrderService.ts:1058:24)

[Actualizar]

Después del comentario de hoangdv , cambié la forma en que estoy validando esto:

 const itemResult = await Promise.all(order.items.map(async (item) => { return await this.itemRepository.getById(item.id) })) itemResult.forEach((item) => { if(!item){ throw new NotFoundError('Item not found in database') } })

Y cambió la prueba para agregar rejects la propiedad Jest:

 await expect(async () => { await orderService.createOrder(order) }).rejects.toThrow(NotFoundError)
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda