Testing FileSaver in Angular 5

不想你离开。 提交于 2020-03-02 00:28:54

问题


I have imported file-saver in angular 5 application, when I am executing test cases it is giving following error:

TypeError: FileSaver.saveAs is not a function

Spec.ts:
import FileSaver from 'file-saver';

.ts
import FileSaver from 'file-saver';
FileSaver.saveAs(blob, filename);

How to mock filesaver in test cases.


回答1:


This works in Angular 7, so I'm guessing it will work in NG5. The first thing is to import FileSaver using the 'as' syntax in both the component and the spec:

import * as FileSaver from 'file-saver';

Then in the spec beforeEach:

spyOn(FileSaver, 'saveAs').and.stub();


来源:https://stackoverflow.com/questions/49479501/testing-filesaver-in-angular-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!