1

I am using QUnit to test my typescript code and everything is fine when I run a simple example like this: http://thomasardal.com/testing-typescript-with-typescript-using-qunit-and-chutzpah/

But my nightmare start when I try create unit tests for my SPA app. At the moment to run the testing using Chutzpah on my VS I got a strange error: "Can't Find variable home in mypath\home.tests.ts(line6).

My Code bellow:

home.ts

import logger = module('services/logger');
export var title = 'Home View';

export function activate() {
    logger.log('Home View Activated', null, 'home', true);
    return true;
}

home.tests.ts

/// <reference path="../../Scripts/qunit.d.ts" />
QUnit.module("home.ts tests");
import home = module("home");

test("test title from home viewmodel", function () {

    // Calling to active public function from home viewmodel. (home.ts)   
    var activateResult:string = home.title;

    // Assert
    equal(activateResult, "Home View", "Result should be Home View ");

});

here you are my typeScript settings: setting for typescript

any idea what is wrong with my code?

UPDATE 1 The complete message from output windows in Vs2012 is:

Test 'home.ts tests:test activate function from home viewmodel' failed Died on test #1 at file:///C:/Users/rolando/AppData/Local/Microsoft/VisualStudio/11.0/Extensions/kyo4ap1e.tvo/TestFiles/QUnit/qunit.js:412 at file:///D:/Mercatus/SourceCode/KILN/AquaVet2/SW/AquaVet.Web/App/viewmodels/_Chutzpah.7.home.tests.js:6: Can't find variable: home in D:\Mercatus\SourceCode\KILN\AquaVet2\SW\AquaVet.Web\App\viewmodels\home.tests.ts (line 6)

0 passed, 1 failed, 1 total (chutzpah).

UPDATE 2 As you see in the code i am trying load home.ts using the keyword module("home").... I am not sure if that could be the reason of my problems. A better solution could be add a internal reference to home.ts

Adding home.ts as internal reference

but i don't know how I can reference to activate function !!.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rolando
  • 752
  • 1
  • 14
  • 41
  • Is your js console showing an 404s? – basarat May 09 '13 at 15:15
  • I am using Chutzpah to run my tests on Vs2012. the result of my tests are available on output window (see the log in my update of the question) – Rolando May 09 '13 at 15:22
  • 1
    Just adding an `import` statement doesn't include an AMD module - you need a loader such as require.js to do that. I'm not sure how QUnit works - but is it capable of loading the `home` module? Without that, you would get precisely the error you describe. Note that the article you refer to uses references like `///` instead. – Jude Fisher May 09 '13 at 16:06
  • the reason because i did use import is because home.ts was not build using a class in order to use something like: var myHome= new Home(); is there some way to use this "public" activate function using /// ?? – Rolando May 09 '13 at 16:17
  • All the work I've done has been with the AMD/require.js format, so I'm not sure, but I think you would need to change the AMD compiler switch in your TS settings, and *maybe* lose the export keyword (not sure about this last - I don't really know how non-AMD modules work). – Jude Fisher May 09 '13 at 16:36
  • @JcFx "Just adding an import statement doesn't include an AMD module - you need a loader such as require.js to do that." is *the* answer. Post it as such and I will vote on it :) – basarat May 09 '13 at 21:37
  • @BASarat how I should add a require.js to my tests? import require = module("require"); ? how? – Rolando May 09 '13 at 21:57
  • 1
    @Rolando you will have to create your own bootstrapper : http://www.youtube.com/watch?v=4AGQpv0MKsA Alternatively you can use something like gruntjs : http://jaketrent.com/post/run-requirejs-with-gruntjs/ Sorry for not being overly detailed. It will depend on your requirements – basarat May 09 '13 at 23:43
  • I am creating my own bootstraper in order to have my html page calling to my bootstraper..but i have one question.. how I can call to my typescript tests from my boostraper. any idea? – Rolando May 10 '13 at 03:39

1 Answers1

4

Just adding an import statement doesn't include an AMD module - you need a loader such as require.js to do that.

A bit of Googling throws up this https://github.com/jrburke/requirejs/wiki/Test-frameworks which may help you get QUnit working with async modules.

And this discussion about using Chutzpah with Require, which links to this example.

Jude Fisher
  • 11,138
  • 7
  • 48
  • 91