0

I get an error when running Jasmine tests: 'ReferenceError: JSZip is not defined'. Here's my controller:

$scope.makeZip = function() {
    var zip = new JSZip();
    zip.file('myPhoto.txt', 'Hello World);
    return 'foo' + zip.generate();
};

And test:

it('should make a zip with correct name', function() {
    var zipFileName = scope.makeZip();
    expect(zipFileName).toMatch('foo');
});

My guess is that I need to mock JSZip constructor somehow: I tried inserting the following at the beginning of the test:

spyOn(window, 'JSZip').andReturn(null);

but then I get 'JSZip() method does not exist'. I thought this is similar problem to Jasmine - How to spy on a function call within a function?, but I couldn't find a right way to fix the issue.

Any ideas? Thanks for all help!

Community
  • 1
  • 1
mmagician
  • 1,970
  • 2
  • 15
  • 26

2 Answers2

0

JSZip is defined on the window. You should not have to pass it into the constructor. In your jasmine page (where you holster your specs), reference jszip.js. If you want, you could create a service..

your angular module.value("JSZip",JSZip);

then pass it into your controller. This is only useful if you wanted to mock JSZip, though.

Onosa
  • 1,275
  • 1
  • 12
  • 28
0

Solved it! I didn't actually want to test JSZip, only to check whether a function makeZip is called correctly.

it('should make a zip with correct name', function() {
    window.JSZip = function() {
            this.file = function() {};
            this.generate = function() {
                return 'bar';
            };
        };
        var zipFileName = scope.makeZip();
        expect(zipFileName).toMatch('foo'); //makeZip should return string containing 'foo'
});
mmagician
  • 1,970
  • 2
  • 15
  • 26