Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular unit test with TypeScript: TS2304: Cannot find name 'module'

I'm trying to write my first Angular unit test in TypeScript and I'm getting the follow error and can not find out why. If any one has any idea please let me know.

TS2304: Cannot find name 'module'.

test code:

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

describe("FooTest", () => {
    beforeEach(module("app"));

});

I'm use TSD (TypeScript Definition manager) to manage my TypeScript definitions.

tsd.json

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {
    "jquery/jquery.d.ts": {
      "commit": "ef32bff4d15782dbbabf99ecb17ba22119cc2bd2"
    },
    "angularjs/angular.d.ts": {
      "commit": "ef32bff4d15782dbbabf99ecb17ba22119cc2bd2"
    },
    "karma-jasmine/karma-jasmine.d.ts": {
      "commit": "ef32bff4d15782dbbabf99ecb17ba22119cc2bd2"
    },
    "jasmine/jasmine.d.ts": {
      "commit": "ef32bff4d15782dbbabf99ecb17ba22119cc2bd2"
    },
    "angularjs/angular-mocks.d.ts": {
      "commit": "ef32bff4d15782dbbabf99ecb17ba22119cc2bd2"
    }
  }
}

I'm use IntelliJ IDEA 14

Thanks, Stefan

like image 622
stevo Avatar asked Oct 02 '15 08:10

stevo


2 Answers

Recently Angular team commented the global module in angular-mocks

//Use `angular.mock.module` instead of `module`, as `module` conflicts with commonjs.
//declare var module: (...modules: any[]) => any;

In order to make your test compile you need to use full namespace, so angular.mock.module

example:

beforeEach(function () {
  angular.mock.module('app');
}
like image 121
Luca Trazzi Avatar answered Nov 16 '22 04:11

Luca Trazzi


before:

beforeEach(function () {
  module('app');
}

after:

beforeEach(function () {
  angular.mock.module('app');
}
like image 21
ton.yeung Avatar answered Nov 16 '22 03:11

ton.yeung