I am facing an issue while testing a method in Angular using Jasmine/Karma. The error message I keep encountering is:
TypeError: undefined is not iterable (cannot read property Symbol (Symbol.iterator))
This is how I created the method:
myMethod(locs: MyCustomType1[], clocs: MyCustomType2[]) {
clocs = clocs
.filter(cl => cl !== null && cl.l_ids !== null);
locs = locs
.filter(l => l !== null && l.id !== null);
clocs.forEach(
cl => {
cl['l_names'] = [];
locs.forEach(
l => {
if (cl.l_ids.includes(l.id)) {
clocs['l_names'].push(l.name);
}
}
);
}
);
}
Here is the current framework of my test:
describe('#MyMethod', () => {
beforeEach(() => {
component.clocs = mockClocs;
component.locs = mockLocs;
component.myMethod(mockLocs, mockClocs);
});
describe('myMethod()', () => {
it('Should check if clocs and locs arrays are defined', () => {
expect(component.clocs).toBeDefined();
expect(component.locs).toBeDefined();
});
it('Should verify that clocs array contains "Location2" and "Location3" with locationIds 2, 3', () => {
expect(component.clocs[1]['l_names'].includes('Location2')).toBeTruthy();
expect(component.clocs[1]['l_names'].includes('Location3')).toBeTruthy();
});
});
});
The error mentioned above persists for every expect() statement within the it() block. Although the logged arrays display the correct values, the expect() function returns undefined. What could be causing this issue?