如何替换 jest.spyOn().and.returnValue()?

mrs*_*nce 6 jasmine jestjs angular nrwl

将 Angular(通过 Nx/NRWL)从 11 升级到 12 时,测试开始失败,原因如下:Property 'and' does not exist on type 'SpyInstance<{ afterClosed: () => Observable<{ snoozeDate: Moment; snoozeNote: string; }>; }, []>'.

有大约 30 个测试使用了 .and.returnValue。

例子:

  • jest.spyOn(mockMatDialog, 'open').and.returnValue({ afterClosed: () => of(true)});,
  • jest.spyOn(component.randomHelperService, 'myHelper').and.returnValue('12354');

替换这种“returnValue”行为的最佳方法是什么?仅供参考,我也有这个设置,testRunner: 'jest-jasmine2',以便升级后其他语法可以工作。

dal*_*ows 15

您可以更改testRunner背面并使用mockReturnValue代替and.returnValue

jest.spyOn(mockMatDialog, 'open').mockReturnValue({ afterClosed: () => of(true)});
jest.spyOn(component.randomHelperService, 'myHelper').mockReturnValue('12354');
Run Code Online (Sandbox Code Playgroud)

我不确定它何时改变,但我必须处理同样的问题。这就是解决方案