AfroMogli
AfroMogli

Reputation: 587

TypeScript + Chutzpah + Jasmine - How to include dependencies in unit test?

I'm running Visual Studio 13 with the Chutzpah test runner and the Jasmine-library. I want to test class A which has dependencies to class B. Class A also has dependencies to JQuery so my code looks something like this:

/// <reference path="../typings/jquery/jquery.d.ts"/>
/// <reference path="../B.ts"/>

describe("A", () => {
    it("can be instaniated", () => {
        var instance = new A();
    });
});

But I'm getting error when I run the test-runner that says: Error: ReferenceError: Can't find variable: $ So it seems like the test-runner cannot resolve the typing include for JQuery?

I have tried to add the following code:

/// <reference path="../jquery-2.1.1.js"/>

But then the compiler complains about that only ".ts" files are valid?! So what am I missing here? I thought that it was OK to include JS-files in TS-files? And how come I need -blocks in the Jasmine-tests but not anywhere else in the application (the compiler resolves the dependencies automatically)?

Is there a "standard way" of resolving dependencies when running Chutzpah + Jasmine in TypeScript?

Upvotes: 2

Views: 3171

Answers (3)

AfroMogli
AfroMogli

Reputation: 587

The standard solution was to use the chutzpah.json-config that comes with Chuzpah, now the dependecies work like a charm! https://chutzpah.codeplex.com/wikipage?title=Chutzpah.json%20Settings%20File

Upvotes: 1

Nick Baker
Nick Baker

Reputation: 727

Can you use chutzpah_reference instead of reference here?

Alternate Reference Syntax

The reference syntax is also used by the TypeScript compiler for referencing files. This can cause an issue since Chutzpah expects you to reference your implementation files and TypeScript excepts you to reference your definition files. To mitigate this issue you can use a Chutzpah specific version of the reference command:

/// <chutzpah_reference path="someDependency.js" />

This works the same as the other syntax except that TypeScript (and VS) will ignore it.

From https://chutzpah.codeplex.com/wikipage?title=Chutzpah%20File%20References

Upvotes: 0

Fenton
Fenton

Reputation: 251152

It is perfectly valid to call JavaScript from TypeScript.

In your case, you can do one of two things...

  1. add declare var $: any;
  2. Download the definition for jQuery from Definitely Typed

I recommend the second option as you'll get proper compiler checks and auto-completion.

You can grab the Definitely Typed definition using NuGet, which makes it super simple.

Additionally, you need to remember to include the actual script at runtime (and in the correct order, i.e. the script tag below needs to appear before you attempt to use jQuery)...

<script src="jquery.js"></script>
<script src="a-script-that-depends-on-jquery.js"></script>

The reference comments are purely a design/compile time tool and don't ensure those scripts will be loaded when the program runs.

Upvotes: 1

Related Questions