visual-studioangulartypescriptjasminechutzpah

Typescript Jasmine Spec compilation removes import statement - Visual Studio


Haven't had much luck finding a solution to this and have been at it for a few hours.

So I have a few folders which contain Angular 2 components/modules/services. I'm adding a new folder for Jasmine test specs to test my resulting JS code.

In Visual Studio i'm importing Jasmine as a reference (nu-get) and also have it pulled in via typings and am using the Chutzpah extension to have the spec.js files show up in the Visual studio test explorer. This all works fine and the most basic tests (true == true and true == false) all return the expected output.

The issue occurs when I try to add a service from my Angular 2 app into the spec to test it out. Here is the code...

import { TestingService } from "../Services/testing.service";

describe("TestingService", function () {
    let TestingService: TestingService;
    TestingService.Testing("TEST"); 

    it("testing is working", function () {
        expect(true).toBe(true);
    });
});

The code compiles correctly, no errors are thrown during the typescript compilation from TS to JS. The issue occurs when I try to run the test the resulting JS code looks like the following...

"use strict";

describe("TestingService", function () {
    var TestingService;
    TestingService.Testing("TEST");
    it("testing is working", function () {
        expect(true).toBe(true);
    });
});

As you can see the testing service import from the TS code has been removed by the compiler and the test case errors out stating that TestService.Testing is undefined. I don't really understand why the import is removed here since all my other angular 2 components/services/models/etc are compiled using the exact same gulp script and the imports carry over.

Here are some other things i've tried with no success.

Looks to be deprecated in typescript now -

/// <reference path="../references.ts" />

Does not work, compilation errors -

import foo = require("../Services/testing.service"); var bar = foo;

Just to reiterate my environment -

VS - Visual Studio

VS Chutzpah searches for .spec.js test cases

VS Jasmine reference in project

Typings Jasmine

Gulp compiles TS to JS - working

Error -

Gulp compiles test specs to JS and imports are removed

Thanks for your time.


Solution

  • Do you have a chutzpah.json file setup? Have you looked at the Chutzpah Angular2 sample. This may help bootstrap you and get you started: https://github.com/mmanela/chutzpah/tree/master/Samples/Angular2/Basic