I've been getting an error while trying to unit test a new method on my service that returns an object that looks like this:
{
requestHeaders: [
{
'header1': '...',
'header2': '...',
'header3': '...'
},
{
'header1': '...',
'header2': '...',
'header3': '...'
},
{
'header1': '...',
'header2': '...',
'header3': '...'
},
]
}
My service method:
const requestObject = this.otherService.getRequestObject(payload);
const [request] = requestObject.headers.attribute; // This doesn't look good
if(request){
// Do stuff
}
When I run npm run test
TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
32 | );
> 33 | const [request] = requestObject.headers.attribute;
| ^
My breaking test:
it('should call sendNewRequest() ', async () => {
jest
.spyOn(myService, 'isExistingRequest')
.mockImplementation(() => Promise.resolve(false));
jest
.spyOn(myService, 'createNewRequest')
.mockImplementation();
await myService.handleRequest({});
expect(myService.createNewProductionOrder).toHaveBeenCalled();
});
});
I think that my getRequestObject is returning undefined because I'm sending an empty object to my handleRequest method, and getRequestObject is expecting a different thing. Now, have in mind that the object I exemplified here is not exactly as the original object is (the original one is much larger and have hundreds of fields), but it is an object with an array of objects.
How should I approach this since I want to avoid creating manually a requestObject inside my testing file?
It is worth mentioning that my getRequestObject method is declared like this in an utils file:
export const objectDtoTransformer = (params) => {doStuff}