Skip to content

Commit d202e27

Browse files
committed
Fleshed out some other ways to write tests with Nodeunit.
1 parent da99565 commit d202e27

File tree

2 files changed

+97
-12
lines changed

2 files changed

+97
-12
lines changed

nodeunit/nodeunit-tests.ts

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ var block: () =>{
1414
};
1515

1616
export var testGroup: nodeunit.ITestGroup = {
17-
setUp: function (callback: nodeunit.ICallbackFunction) {
17+
setUp: (callback) => {
1818
callback();
1919
},
20-
tearDown: function (callback: nodeunit.ICallbackFunction) {
20+
tearDown: (callback) => {
2121
callback();
2222
},
2323
test1: function (test: nodeunit.Test) {
@@ -55,5 +55,83 @@ export var testGroup: nodeunit.ITestGroup = {
5555

5656
test.done(error);
5757
test.done();
58+
},
59+
"This is a test with a nice description": (test: nodeunit.Test) => {
60+
test.done();
5861
}
5962
};
63+
64+
65+
// see https://github.com/caolan/nodeunit/blob/master/examples/nested/nested_reporter_test.unit.js for example.
66+
// (https://github.com/caolan/nodeunit/commit/9fee91149324f79753eadbcf8993399a7d76da40)
67+
68+
69+
70+
var testCase = nodeunit.testCase;
71+
72+
export var testCaseGroup = testCase({
73+
"Test 0.1": function(test: nodeunit.Test) {
74+
test.ok(true);
75+
test.done();
76+
},
77+
78+
"TC 1": testCase({
79+
"TC 1.1": testCase({
80+
"Test 1.1.1": function(test: nodeunit.Test) {
81+
test.ok(true);
82+
test.done();
83+
}
84+
})
85+
}),
86+
87+
"TC 2": testCase({
88+
"TC 2.1": testCase({
89+
"TC 2.1.1": testCase({
90+
"Test 2.1.1.1": function(test: nodeunit.Test) {
91+
test.ok(true);
92+
test.done();
93+
},
94+
95+
"Test 2.1.1.2": function(test: nodeunit.Test) {
96+
test.ok(true);
97+
test.done();
98+
}
99+
}),
100+
101+
"TC 2.2.1": testCase({
102+
"Test 2.2.1.1": function(test: nodeunit.Test) {
103+
test.ok(true);
104+
test.done();
105+
},
106+
107+
"TC 2.2.1.1": testCase({
108+
"Test 2.2.1.1.1": function(test: nodeunit.Test) {
109+
test.ok(true);
110+
test.done();
111+
},
112+
}),
113+
114+
"Test 2.2.1.2": function(test: nodeunit.Test) {
115+
test.ok(true);
116+
test.done();
117+
}
118+
})
119+
})
120+
}),
121+
122+
"TC 3": testCase({
123+
"TC 3.1": testCase({
124+
"TC 3.1.1": testCase({
125+
"Test 3.1.1.1 (should fail)": function(test: nodeunit.Test) {
126+
test.ok(false);
127+
test.done();
128+
}
129+
})
130+
})
131+
})
132+
});
133+
134+
135+
136+
137+

nodeunit/nodeunit.d.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
// Imported from: https://github.com/soywiz/typescript-node-definitions/nodeunit.d.ts
77

88
declare module 'nodeunit' {
9+
export interface ITestCase {
10+
(testCase: {[property: string]: ITestBody | ITestGroup | void}) : void;
11+
}
12+
export var testCase : ITestCase;
13+
914
export interface Test {
1015
done: ICallbackFunction;
1116
expect(num: number): void;
@@ -31,15 +36,15 @@ declare module 'nodeunit' {
3136

3237
// Test Group Usage:
3338
// var testGroup: nodeunit.ITestGroup = {
34-
// setUp: function (callback: nodeunit.ICallbackFunction): void {
35-
// callback();
36-
// },
37-
// tearDown: function (callback: nodeunit.ICallbackFunction): void {
38-
// callback();
39-
// },
40-
// test1: function (test: nodeunit.Test): void {
41-
// test.done();
42-
// }
39+
// setUp: (callback) => {
40+
// callback();
41+
// },
42+
// tearDown: (callback) => {
43+
// callback();
44+
// },
45+
// test1: (test: nodeunit.Test) => {
46+
// test.done();
47+
// }
4348
// }
4449
// exports.testgroup = testGroup;
4550

@@ -48,12 +53,14 @@ declare module 'nodeunit' {
4853
}
4954

5055
export interface ITestGroup {
56+
/** The setUp function is run before each test */
5157
setUp?: (callback: ICallbackFunction) => void;
58+
/** The tearDown function is run after each test calls test.done() */
5259
tearDown?: (callback: ICallbackFunction) => void;
60+
[property: string] : ITestGroup | ITestBody | ((callback: ICallbackFunction) => void);
5361
}
5462

5563
export interface ICallbackFunction {
5664
(err?: any): void;
5765
}
5866
}
59-

0 commit comments

Comments
 (0)