I am new to Jest and i am trying test async await. And I am trying to cover Json.Parse and it is throwing an exception like below mentioned.
Error Message
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
Code
public async callDataSourceCommand(dialogData: any, RecipeId: string) {
const gridItems = await this.dataSourceService.myPromiseMethod(id, collection);
this.updateGrid(JSON.parse(gridItems));
}
private updateGrid(gridItems: any) {}
Mock Data
public get dataSourceServiceMock(): any = {
return {
myPromiseMethod: function () {
return Promise.resolve({
selectedOrder: {
earlierStartTime: '2/5/2020',
__id: 'orderId123'
},
Collection: [{
__id: 'b1order 1',
resName: 'New recipe_V1.0',
plannedQuantity: '3',
resId: 'ns=6;s=4/ProjectData/1',
actualQuantity: '1',
description: 'batchDesc',
}]
});
}
}
}
Test Suite
it('1. Should execute ', async() => {
const myDialogApp: DialogApp = TestBed.get(DialogApp);
myDialogApp.selectedOrder = selectedOrder;
myDialogApp.RecipeId = Recipe.__id;
jest.spyOn(dataSourceServiceMock, 'myPromiseMethod');
await myDialogApp.callDataSourceCommand(dialogData, RecipeId);
expect(dataSourceServiceMock.myPromiseMethod).toHaveBeenCalled();
});