How to get the definition of “Module” in TypeScript inside the Jasmine Unit Test?

So this is really stupid. But this is not possible for Google, because the undefined module is literally module. I got rid of all other TS errors in my unit tests, except for this last one:

main / components / login / loginController.Spec.ts (12,7): error TS2304: cannot find the name 'module'.

Some said, “Just enable angular -mocks.js,” but I did it and it fixed some other errors, but not this one.

Here are my links:

/// <reference path="../../typings/angularjs/angular.d.ts" />
/// <reference path="../../typings/jquery/jquery.d.ts" />
/// <reference path="../../typings/d3/d3.d.ts" />
/// <reference path="../../typings/jasmine/jasmine.d.ts" />
/// <reference path="../../typings/angularjs/angular-mocks.d.ts" />

And here is a simple test illustrating the problem.

describe('Login Controller Spec', function() {
    beforeEach(function() {
      // uh oh
      module('App');
      inject(function(_$controller_, $rootScope) {
        scope = $rootScope.$new();
        controller = _$controller_;
        navCtrl = controller('loginController', { $scope: scope});
      });

    });

    it('should call init', function(){
         //...
    });
  });

Any other libraries for which I should get definitions?

+4
source share
1

angular.mock.module('App');  

-

  window.module = angular.mock.module = function() {
    var moduleFns = Array.prototype.slice.call(arguments, 0);
    return isSpecRunning() ? workFn() : workFn;
    /////////////////////
    function workFn() {
      if (currentSpec.$injector) {
        throw new Error('Injector already created, can not register a module!');
      } else {
        var fn, modules = currentSpec.$modules || (currentSpec.$modules = []);
        angular.forEach(moduleFns, function(module) {
          if (angular.isObject(module) && !angular.isArray(module)) {
            fn = function($provide) {
              angular.forEach(module, function(value, key) {
                $provide.value(key, value);
              });
            };
          } else {
            fn = module;
          }
          if (currentSpec.$providerInjector) {
            currentSpec.$providerInjector.invoke(fn);
          } else {
            modules.push(fn);
          }
        });
      }
    }
  };

window['module']('App'); // and maybe also window.module('App');
+3

Source: https://habr.com/ru/post/1616823/


All Articles