Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!