karma.conf.js uncaught referencerror: google no defined karma.conf.js uncaught referencerror: google no defined angularjs angularjs

karma.conf.js uncaught referencerror: google no defined


You get this error because you use google maps v3 API on your application but do not have any code, which will initialize window.google property, when you run your tests.

Solution 1

Compile this mock to javascript and reference compiled *.js in files of your config.

Solution 2

  1. Create your own google-mock.js
  2. Add it to files array of your karma.config
  3. Add stab to google-mock.js for each Google Maps API call which you use in your applike in example below:

    (function(){  window.google = window.google || {    maps: {      Map: function(){},// and so on...    }  };})();

Solution 3

  1. Open this link
  2. Save ALL text from this page to file google-maps-api.js
  3. Add path to this file to your files array in karma.config


In my case in the files section of karma.conf.js I needed the google api with "js" extension (not .ts!):

 files:[          'src/app/shared/mocks/google-api.js'      ],

I have copied and pasted the real google API from the url:"https://maps.googleapis.com/maps/api/js?sensor=false"

If you pass the url it won´t work.It´s not the a cool solution, but for now it works at least.