2
votes

I'm newbie in unit test, I'm using angularjs with typescript. For unit testing I'm using jasmine and chutzpah but I meet error "ReferenceError: Can't find variable: angular"

Test file such as

//File:test.ts
///<chutzpah_reference  path="../Scripts/angular.js"/>
///<chutzpah_reference  path="../Scripts/angular-mocks.js"/>
///<chutzpah_reference  path="../Scripts/jasmine/jasmine.js"/>
///<reference path="../scripts/typings/angularjs/angular.d.ts"/>
///<reference path="../scripts/typings/angularjs/angular-mocks.d.ts"/>
///<reference path="../scripts/typings/jasmine/jasmine.d.ts"/>
///<reference path="../controller/testController.ts"/>

module app {
    describe("testwithjasmine", () => {
        var testController: Testcontroller;
        beforeEach(()=> {
            testController = new Testcontroller();
        })
        //This test OK
        it("Test controller", () => {
            expect(testController.tilte).toBe("title");
        })
    })

    //Error with case below
    describe("testInject", () => {
        var scope, controller;
        beforeEach(angular.mock.module("app"));
        beforeEach(() => inject(($controller, $scope) => {
            scope = {};
            controller = $controller('testController', { $scope: scope });
        }));
        it("test contrller2", () => {
            expect(scope.title).toEqual("title");
        })
    })
}

chutzpah.json content

{
"Compile": {
    "Mode": "External",
    "Extensions": [".ts"],
    "ExtensionsWithNoOutput": [".d.ts"]
   },
  "References": [
    {
      "Includes": [
        "*/Controller/*.ts"
      ],
      "Excludes": [
        "*/Scripts/typings/jasmine/*.d.ts",
        "*/scripts/typings/angularjs/*.d.ts"
      ]
    }
  ],
  "Tests": [
    {
      "Includes": [ "*/Test/*.ts" ],
      "Excludes": [
        "*/Scripts/Tests/*.d.ts"
      ]

    }
  ]
}

Any suggestion? Thanks!

1
Try to add after "*/Controller/*.ts" another row: */Scripts/angular.js - Slava Utesinov
Thanks @SlavaUtesinov I added "*/Scripts/angular.js" but not work. - mrlive

1 Answers

0
votes

chutzpah_reference does not work (returns empty content) when your tests are written in typescript.

I ended up hosting my *.js files in a separate web application (say localhost:9090), also created custom harnass file (VS > R# > Options > Tools > Unit testing > JavaScript Tests) and added script tag to include the library scripts.

<script src="http://localhost:9090/lib/angular.js" ></script>

Any attempt to use R# failed miserably, e.g. commonjs won't work because of all kind of limitations (won't even help is you use custom harnass).

A best alternative I know of within VS (but without R#) is to use custom html file which includes all scripts and host the file 'manually'.

Personally I am in the process of looking for other alternative that doesnt need resharper or even VS.