Testing TypeScript using Jasmine and Chutzpah within Visual Studio 2015

I am new to unit testing in AngularJS with TypeScript. I have been using jasmine and chutzpah for unit testing, but I encountered an error "ReferenceError: Can't find variable: angular".

My test file looks like this:

//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");
        })
    })
}

The content of my chutzpah.json file is as follows:

{
"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 suggestions on how to resolve this issue would be greatly appreciated. Thank you!

Answer №1

When using typescript for tests, the chutzpah_reference feature may not work properly and return empty content.

To work around this issue, I had to host my *.js files on a separate web application at localhost:9090. I also created a custom harnass file in Visual Studio by going to Resharper > Options > Tools > Unit Testing > JavaScript Tests and added script tags to include the necessary library scripts.

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

Attempts to use Resharper failed due to limitations with commonjs, even with a custom harnass setup.

An alternative solution within Visual Studio (without Resharper) is to create a custom html file that includes all scripts and manually host the file.

Currently, I am exploring other alternatives that do not rely on Resharper or even Visual Studio.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding a total property at the row level in JavaScript

Here is a JavaScript array that I need help with: [{ Year:2000, Jan:1, Feb: }, {Year:2001, Jan:-1, Feb:0.34 }] I want to calculate the total of Jan and Feb for each entry in the existing array and add it as a new property. For example: [{ Year:2000, Ja ...

Resolving circular dependencies caused by APP_INITIALIZER

My AuthenticationService is responsible for loading the AngularFirestore and is loaded in the RootComponent. All app modules are lazily loaded within the RootComponent (which contains the main router-outlet). However, several sub-modules also load the Ang ...

Activate expansive pop-up windows with primeng's dynamic dialog feature

In my Angular web application, I am using the PrimeNg modal extension to display modal popups. I have successfully passed a component to the modal service with the following code: const ref = this.dialogService.open(LogsComponent, { data: { ...

Steps to alter background image and adjust its height upon function activation

I am working on a search page with an advanced search option that only certain users can access. I need the height of my div to increase accordingly and also change the background image to a larger size when the advanced search is selected. How can I make ...

Angular is known for sending only the fields that have changed to the update method

I need help with optimizing my save method. When a user clicks SAVE, I only want to send the fields that have been changed instead of all 50+ fields on the page. This will reduce the amount of data being sent every time. Api.Admin.update({ obsoleteDat ...

Upon updating AngularFire, an error is thrown stating: "FirebaseError: Expected type 'Ea', but instead received a custom Ta object."

I have recently upgraded to AngularFire 7.4.1 and Angular 14.2.4, along with RxFire 6.0.3. After updating Angular from version 12 to 15, I encountered the following error with AngularFire: ERROR FirebaseError: Expected type 'Ea', but it was: a c ...

When running a callback function, the "this" of an Angular 2 component becomes undefined

One issue I'm facing is with a component that fetches data from a RESTful endpoint using a service, which requires a callback function to be executed after fetching the data. The problem arises when attempting to use the callback function to append t ...

What is the process for specifying a data type for a pre-existing npm package module?

I am currently working on converting a codebase that utilizes nodemailer along with the nodemailer-html-to-text plugin to TypeScript. While nodemailer has @types definitions available, the same is not true for nodemailer-html-to-text. How can I go about ...

Exploring the Usage of Jasmine Testing for Subscribing to Observable Service in Angular's OnInit

Currently, I am facing challenges testing a component that contains a subscription within the ngOnInit method. While everything runs smoothly in the actual application environment, testing fails because the subscription object is not accessible. I have att ...

Within the ng-repeat loop, every switch button undergoes a status change

Utilizing ng-repeat, I have implemented a feature to display multiple content with on/off buttons. However, when toggling the off button for a specific content, all button states are being changed instead of just the selected one. <div ng-repeat="setti ...

When the file is active on a local machine, the bot commands run smoothly. However, these commands do not execute on a remote

Lately, while working on coding a discord bot using discord.js, I came across an issue. Whenever I run my bot on my local machine, all the commands work perfectly fine. However, after committing and pushing the code to GitHub, and then allowing buddy.works ...

Angular 2 - resolving the access control allow origin header issue

My project utilizes Spring on the back-end and Angular2 on the front-end. In the webapp folder of my Spring project, there is a JSON file that I am attempting to access from Angular. I can successfully access the file by directly typing "http://localhost: ...

Tips for storing an array of ReplaySubjects in a single variable in an Angular application

I am looking to store several ReplaySubjects in a single array. Here is my code: public filteredSearch: ReplaySubject<any[]> = new ReplaySubject(1); this.filteredSearch[id].next(filter(somedata)); When I run this code, I encounter an error saying ...

Expo background fetch initialized but not activated

During the development of my React Native app, I encountered the need to perform periodic background fetches from another server. To achieve this, I utilized two classes from Expo: import * as BackgroundFetch from 'expo-background-fetch'; import ...

What is the process for retrieving an element from component interaction?

Is there a way to dynamically change the background color based on component interaction? I am looking for a method to compare the target element with the current element. For example, here is a hypothetical scenario: <span [style.background]=" ...

Swiping in Angular2 gets a new twist with Swiper typings

Having trouble importing typings for Swiper into my Angular 2 project. After installing Swiper and its typings using npm, I tried including Swiper in my component like this: import { Swiper } from 'swiper'; However, Atom displays an error: ...

Looking to save a CSS element as a variable

I am working on improving the readability of my code in Protractor. My goal is to assign a CSS class to a variable and then use that variable within a click method. element.all(by.css("div[ng-click=\"setLocation('report_road')\"]")).cl ...

Passing an array as a parameter to a custom filter in AngularJS

While creating a custom filter in Angularjs, I am passing the data set I want to filter and an array as the filter values: Array of Toggle Button States Filter Parameter Array Value How the filter is invoked: <div class="row" ng-repeat=&quo ...

Toggle button color on click by using ng-click on a different button

As a newcomer to Angular, I am facing an issue. I have two buttons - button1 and button2. What I want is that when I click on button1, its color changes from green to red. Then, when I click on button2, the color of button1 should revert back to green. How ...

Combining jQuery MixItUp with AngularJS's NgRoute for seamless functionality

Successfully incorporated jQuery MixItUp into my AngularJs application. The content to be displayed with MixItUp is loaded from custom services. After fetching all items, I trigger the instantiation of jQuery MixItUp. I am also utilizing AngularJS NgRout ...