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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With