8

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

2
  • just don't run tsc on that spec / mocks / d.ts file? or define var module: any; Commented Oct 2, 2015 at 8:07
  • @YOU sorry I don't understand what you mean by just don't run tsc on that spec / mocks / d.ts file? that. Commented Oct 2, 2015 at 8:14

3 Answers 3

16

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');
}
Sign up to request clarification or add additional context in comments.

3 Comments

I encountered exactly this issue, this worked perfectly. Thanks!
I get the issue where it says that angular doesn't have a property "mock" on it, is there some definition file I'm missing? I have angular-mock that's already on dt, not sure if I need another one
I have angular-mocks, with the final S, is this what you got or angular-mock?
5

before:

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

after:

beforeEach(function () {
  angular.mock.module('app');
}

Comments

0

My guess:

you are using angular-mocks.d.ts but I don't see angular-mocks.ts anywhere.

d.ts files contain only the type definitions, not the actual implementation. So you will need both.

PS: I think that angular-mocks.ts has only the .js version :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.