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

117
Views
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 answers
Answer question

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 Report
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!