Reputation: 69
I am new to jest and i am trying to call await function it return the promise. But i am getting and error like expected calls 1 and received calls is 0
Code:
public async callDataSourceCommand(dialogData: any, RecipeId: string) {
const gridItems = await this.dataSourceService.myPromiseMethod(id, collection);
}
MockData
public get dataSourceServiceMock(): any = {
return {
myPromiseMethod: function () {
return Promise.resolve({
id: '123',
collection: []
});
}
}
}
Test suite
it('1. Should execute ', async() => {
const myDialogApp: DialogApp = TestBed.get(DialogApp);
myDialogApp.selectedOrder = selectedOrder;
myDialogApp.RecipeId = Recipe.__id;
myDialogApp.callDataSourceCommand(dialogData, RecipeId);
jest.spyOn(dataSourceServiceMock, 'myPromiseMethod');
expect(dataSourceServiceMock.myPromiseMethod).toHaveBeenCalled();
});
After adding shuan's comment, still i am facing an issue is like,
console.error node_modules/zone.js/dist/zone.js:703 Unhandled Promise rejection: Unexpected token o in JSON at position 1 ; Zone: ProxyZone ; Task: Promise.then ; Value: SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () at OrderManagementMultipleBatchesDialogApp. (D:\DCS_WorkSpace\src\DCSPlus\UI\libs\order-management\apps\src\components\order-management-multiple-batches-dialog-app\order-management-multiple-ba tches-dialog-app.factory.ts:102:30)
i have updated the test case
MockData
public get dataSourceServiceMock(): any = {
return {
myPromiseMethod: function () {
return Promise.resolve({
selectedOrder: {
earlierStartTime: '2/5/2020',
__id: 'orderId123'
},
batchCollection: {
__id: 'b1order 1',
masterRecipeName: 'New recipe_V1.0',
plannedQuantity: '3',
masterRecipeId: '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(multipleBatchData, masterRecipeId);
expect(dataSourceServiceMock.myPromiseMethod).toHaveBeenCalled();
});
Upvotes: 1
Views: 2088
Reputation: 141542
As Jaromanda correctly wrote, you need to await
the asynchronous method. Also, you need to spy on the method before acting not after acting.
Here is a simplified, standalone version of your original example that you can run in Jest.
class MyDialogApp {
constructor(private dataSourceService: any) {}
public async callDataSourceCommand() {
await this.dataSourceService.myPromiseMethod();
}
}
const dataSourceServiceMock = {
myPromiseMethod: function() {
return Promise.resolve({
id: '123',
collection: []
});
}
};
const myDialogApp = new MyDialogApp(dataSourceServiceMock);
it('1. Should execute ', async () => {
// arrange
jest.spyOn(dataSourceServiceMock, 'myPromiseMethod');
// act
await myDialogApp.callDataSourceCommand();
// assert
expect(dataSourceServiceMock.myPromiseMethod).toHaveBeenCalled();
});
Upvotes: 2