4

I've recently tried to incorporate qUnit and Chutzpah to unit test a project I'm working on which is written in typescript.

I've got things setup in what I believe is the correct manner and the following things work:

  • The typescript application! - I can run a very early version of the application
  • Chutzpah - I installed this on VS2012 and it correctly see's my qUnit demo test
  • qUnit - Appears installed and works with Chutzpah, I can run a simple test (one which doesn't look at my code)

With these three in place I presumed I could begin to write tests for the typescript app, as a test I wrote a simple test in typescript:

TreeBurst.Tests.ts

///<reference path="../typings/qunit/qunit.d.ts" />
///<reference path="references.ts">
///<reference path="references.js"> // just in case, unsure what needed referencing here

module DMC.TreeBurst {

QUnit.module("TreeBurst.Node.ts tests");

test("Load-a-single-node-creates-correctly", () => {

    var node = [new DMC.TreeBurst.Node({
        id: 1,
        parentId: null,
        title: ""
    })];

    ok(node, "Node not created correctly when provided valid constructor parameters");

});
}

Unfortunately when I run this I get the following:

'undefined' is not a constructor (evaluating 'new DMC.TreeBurst.Node({
                id: 1,
                parentId: null,
                title: ""
            })')

Now I'm pretty sure it SHOULD be a constructor, but the combination of Chutzpah and qUnit doesn't seem to see it at such.

I've spent some time searching about, but everything I've found suggests that things should just 'work'. Am I doing something obviously wrong here?

Any thoughts greatly appreciated.

EDIT: In response to the comment, this is the class declaration:

/// <reference path="references.ts" />
module DMC.TreeBurst {

export interface NodeOptions {

    // location specific
    id: number;
    parentId?: number;

    // node internals
    title: string;
    content?: string;
    colour?: string;        
}

export class Node {

    public id: number;
    public parentId: number = null;
    public title: string;
    public content: string = null;        
    public depth: number = null;

    public colour: string = null;

    constructor(opts: NodeOptions) {
        //removed from brevity
    }
    // methods removed for brevity
}
}
dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
  • 1
    You should just be able to do new Node(....) since you're already in the TreeBurst namespace. Also did you export your node class? – N. Taylor Mullen Nov 14 '13 at 20:52
  • @N.TaylorMullen Yeah I get the same result with or without it being fully qualfied, I have exported the class too, will edit the question to show that, in case I've done something wrong in there. – dougajmcdonald Nov 14 '13 at 21:02

2 Answers2

1

I was able to get your example working with the following definitions for your test file and code file. I placed all files in the same directory but you can easily move them and change the paths.

TreeBurst.tests.ts

///<reference path="qunit.d.ts" />
///<reference path="TreeBurst.ts" />

module DMC.TreeBurst {

QUnit.module("TreeBurst.Node.ts tests");

test("Load-a-single-node-creates-correctly", () => {

    var node = [new DMC.TreeBurst.Node({
        id: 1,
        parentId: null,
        title: ""
    })];

    ok(node, "Node not created correctly when provided valid constructor parameters");

});
}

TreeBurst.ts

module DMC.TreeBurst {

export interface NodeOptions {

    // location specific
    id: number;
    parentId?: number;

    // node internals
    title: string;
    content?: string;
    colour?: string;        
}

export class Node {

    public id: number;
    public parentId: number = null;
    public title: string;
    public content: string = null;        
    public depth: number = null;

    public colour: string = null;

    constructor(opts: NodeOptions) {
        //removed from brevity
    }
    // methods removed for brevity
}
}
Matthew Manela
  • 16,572
  • 3
  • 64
  • 66
0

Adding a reference to a js file has no effect. Also instead of referencing reference.ts reference the folder http://matthewmanela.com/blog/chutzpah-2-2-with-typescript-support/ this is because of the way chutzpah works (does not compile referred files with --out)

basarat
  • 261,912
  • 58
  • 460
  • 511
  • 1
    Thanks for the reply, I added the .js reference in desperation! to no avail. I've updated the reference to use a folder, or even a reference to the specific DMC.TreeBurst.Node.ts file explicitely but I still get the same response. Are there any other places to look? – dougajmcdonald Nov 14 '13 at 21:16