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

119
Vistas
reduce method fails unit test

I have the following method within an Angular component:

public mapInvoices(invoices: any[]): Record<string, any> {
    return invoices.reduce((map, obj) => {
      map[obj.letterType] = obj;
      return map;
    }, {});
  }

When I run the unit test for this component, I'm getting the following issue:

APAUInvoicesDocumentsComponent › should create

    TypeError: Cannot read property 'reduce' of undefined

      37 |
      38 |   public mapInvoices(invoices: any[]): Record<string, any> {
    > 39 |     return invoices.reduce((map, obj) => {
         |                     ^
      40 |       map[obj.letterType] = obj;
      41 |       return map;
      42 |     }, {});

I could bypass this issue by assigning a default value to the parameter like:

public mapInvoices(invoices: any[] = [])

but I would like to understand why this happens.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To answer your question, it is because mapInvoices is being called with an undefined parameter. By setting a default value you are actually overriding initial state.

public mapInvoices(invoices?: any[] = []) {}
mapInvoices(); // call bombs without a default value
about 4 years ago · Juan Pablo Isaza Denunciar
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