Here is a method that is part of an Angular component:
public mapInvoices(invoices: any[]): Record<string, any> {
return invoices.reduce((map, obj) => {
map[obj.letterType] = obj;
return map;
}, {});
}
During the unit test for this component, an issue has been detected:
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 | }, {});
To workaround this problem, one solution is to set a default value for the parameter like this:
public mapInvoices(invoices: any[] = [])
However, the root cause of this issue is still unclear and warrants further investigation.