Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing FileSaver in Angular 5

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.

like image 454
Sreekanth Reddy Avatar asked Mar 25 '18 18:03

Sreekanth Reddy


1 Answers

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();
like image 143
Craig Avatar answered Sep 27 '22 23:09

Craig