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
}
}
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
}
}
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)
来源:https://stackoverflow.com/questions/19986705/using-chutzpah-to-run-typescript-qunit-tests