Javascript 未使用jest调用异步等待函数
我是个新手,我正在尝试调用wait函数,它将返回承诺。但我收到的错误与预期呼叫1相同,收到的呼叫为0 代码:Javascript 未使用jest调用异步等待函数,javascript,typescript,jestjs,ts-jest,Javascript,Typescript,Jestjs,Ts Jest,我是个新手,我正在尝试调用wait函数,它将返回承诺。但我收到的错误与预期呼叫1相同,收到的呼叫为0 代码: public async callDataSourceCommand(dialogData: any, RecipeId: string) { const gridItems = await this.dataSourceService.myPromiseMethod(id, collection); } 模拟数据 public get dataSourceServiceM
public async callDataSourceCommand(dialogData: any, RecipeId: string) {
const gridItems = await this.dataSourceService.myPromiseMethod(id, collection);
}
模拟数据
public get dataSourceServiceMock(): any = {
return {
myPromiseMethod: function () {
return Promise.resolve({
id: '123',
collection: []
});
}
}
}
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',
}
});
}
}
}
测试套件
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();
});
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();
});
加上舒安的评论后,我仍然面临这样一个问题,
console.error节点_modules/zone.js/dist/zone.js:703
未处理的承诺拒绝:JSON中位置1处的意外标记o;区域:ProxyZone;任务:承诺;Value:SyntaxError:JSON中位置1处的意外标记o 在JSON.parse()处 在OrderManagementMultipleBatchesDialogApp。(D:\DCS\U WorkSpace\src\DCSPlus\UI\libs\order management\apps\src\components\order management多批次对话框app\order management多批次 tches对话框应用程序工厂ts:102:30) 我已经更新了测试用例 模拟数据
public get dataSourceServiceMock(): any = {
return {
myPromiseMethod: function () {
return Promise.resolve({
id: '123',
collection: []
});
}
}
}
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',
}
});
}
}
}
测试套件
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();
});
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();
});
正如Jaromanda正确编写的那样,您需要等待异步方法。此外,你需要在行动前监视方法,而不是在行动后 这里是原始示例的简化、独立版本,您可以开玩笑地运行它
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();
});
看到对异步函数的调用是很奇怪的,例如,
myDialogApp.callDataSourceCommand(dialogData,RecipeId)代码>这不是等待-你确定你知道如何使用async/await吗?嗨,Shuan,谢谢你的回复。我已经更新了您的更改。但我仍然面临上述问题。你能帮我解决这个位置1的JSON中的意外标记o吗;区域:ProxyZone;任务:承诺;Value:SyntaxError:OrderManagementMultipleBatchesDialogApp的JSON.parse()位置1处的JSON中意外标记o。(D:\DCS\u WorkSpace\src\DCSPlus\UI\libs\order management\apps\src\components\order management多批次对话框app\order management多批次对话框app.factory.ts:102:30)